I am attempting to debug a program that seems to be having issues with pointer passing. I have a class defined as below:
class data{
public:
data(int x, int y){
a=x;
b=y;
md = new metadata();
}
~data(){
delete md;
}
int a;
int b;
metadata* md; //metadata is another class
};
and several functions that take a pointer to a data object:
void function1(data* d){
//do stuff
}
void function2(data* d){
//do stuff
function1(d);
}
void function3(data* d){
//do stuff
function2(d);
}
int main(){
//stuff
data* data = new data(1,2);
function3(d);
//other stuff
}
I have a breakpoint set in function1 and one in function3. somewhere between these two breakpoints, md gets reassigned to point to a different metadata object (d, though, still points to the same memory address). There are thousands of lines executed between the two breakpoints, so its not really feasible to just step through, so I want to watch and break when the md pointer is changed. however, watch d->md for the breakboint in function1 does not work, as I get a message saying
Watchpoint 1 deleted because the program has left the block in which its expression is valid
which presumably occurs because in d is local to the context of the function. Is there a way to watch the memory address in which the md pointer is stored to see when it gets changed (i.e. when md is reassigned) and break there?
Get the address of
d->mdusingprint d->mdand watch the expression*((metadata *)0x....)instead of using the named.