How to track down when a dynamic variable changes in debugging visual studio c++?

Chuck Cochems 1 Reputation point
2021-09-16T16:08:27.243+00:00

Some background here.

I have a class. Lets' call it InventoryObject

it's a public subclass of DataObject (this part doesnt' seem to be important)

it has the following variables

float mass, value;
unsigned int type; // INVOBJ_
int status;
long amount;
void *extra; // based on type

the last is the interesting part. it's a pointer to a struct that's different depending on what type is. i just added a new one to the code, InvStabledPet

it is casted as necessary at runtime.

When a new InventoryObject of type INVOBJ_STABLED_PET is created, a new InStabledPet struct is created, and extra is pointed at it.

case INVOBJ_STABLED_PET:
        extra = new InvStabledPet();
        ((InvStabledPet *)extra)->type = 0;
        ((InvStabledPet *)extra)->subType = 0;
        ((InvStabledPet *)extra)->a = 255;
        ((InvStabledPet *)extra)->b = 255;
        ((InvStabledPet *)extra)->r = 255;
        ((InvStabledPet *)extra)->g = 255;
        ((InvStabledPet *)extra)->damageDone = 0;
        ((InvStabledPet *)extra)->defense = 0;
        ((InvStabledPet *)extra)->healAmountPerSecond = 0;
        ((InvStabledPet *)extra)->health = 0;
        ((InvStabledPet *)extra)->magicResistance = 0.0f;
        ((InvStabledPet *)extra)->maxHealth = 0;
        ((InvStabledPet *)extra)->sizeCoeff = 0.0f;
        ((InvStabledPet *)extra)->toHit = 0;
        break;

These are all analogues of the same data for monsters.

The inventories are stored in a doubly linked list, and are read from and written to flat files.

This seems to work, except that at random times, every InvStabledPet has it's type set to zero, EXCEPT the one that was just appended to the top, yet the code i just put there is the ONLY code in the entire source that ever sets type to zero.

There are two routine that introduce new ones of this item.

Both opera the same.

They iterate through the inventory until they hit the null pointer at the end. they check the item the yfound. if it's a INV_STABLED_PET, a pointer gets declared to the struct. The defense of the existing monster is compared wit the item, with the stats updated if the items is lower, with type and sunType remaining unchanged. regardless, if a matching item was found, a bool flag is set to record this (it was false before the loop) the pointer is NULLed, to break out of the while loop. if not, it goes to the next item, and the while loop repeats until it falls off the end. If one was NOT found, then a new one is created, and it's tats are all copied from the monster.

sprintf(tempText, "Stabled %s", monsterData[controlledMonster->type][controlledMonster->subType].name); // say what type it is
iObject = new InventoryObject(INVOBJ_STABLED_PET, 0, tempText);
InvStabledPet * exSp = (InvStabledPet *)iObject->extra;

iObject->mass = 0.0f;
iObject->value = 1000;
iObject->amount = 1;
// copy the stats into it
exSp->a = controlledMonster->a;
exSp->b = controlledMonster->b;
exSp->g = controlledMonster->g;
exSp->r = controlledMonster->r;
exSp->damageDone = controlledMonster->damageDone;
exSp->defense = controlledMonster->defense;
exSp->healAmountPerSecond = controlledMonster->healAmountPerSecond;
exSp->health = controlledMonster->maxHealth;
exSp->magicResistance = controlledMonster->magicResistance;
exSp->maxHealth = controlledMonster->maxHealth;
exSp->sizeCoeff = controlledMonster->sizeCoeff;
exSp->subType = controlledMonster->subType;
exSp->toHit = controlledMonster->toHit;
exSp->type = controlledMonster->type;

// and add it to the inventory list
charInfoArray[curCharacterIndex].inventory->objects.Prepend(iObject);

These list functions have worked fine with all other items ever made, including ones with many more components in the extra.

The save function does not change the item.

The load function create a new one, read s the data in with fscanf, and writes what was actually in the file to the extra properly, and then calls AddItemSorted to place it in it's proper position in the list, which des not change extra (verified)

I need to break when ANY memory that belongs to a InvStabledPet scrust is modified such that type becomes 0 to track down when this is happening. Other than the initial allocation, 0 is never written there by the code. then hwo the heck is this changing? That value is only ever compared in those two routines, and confirmed to be == there in both, so that's not it. It is only ever assigned anythign but the currently controlled monster or what's in the saved file during initial allocation. i CAN'T be changing, yet it somehow is, and i need to see WHAT code changes it.

I'm at a loss for how to proceed.

Because the variable is dynamically allocated, i don't know WHERE to watch in advance or how many. I need to watch ALL of them!

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
2,887 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
771 questions
{count} votes