Help!! My memory gets lost! Why???

Started by
1 comment, last by WuTz 14 years, 2 months ago
Hello!! I've got a really BIG problem! The memory of a buffer gets lost between two function calls! And the crazy thing is, that these functions have NOTHING to do with my buffer. I allocated memory with byte* Data = new byte[Size]; in another class to hold a file. Then I load the data into it, and everything is fine. But then, when I want to do some UI-Stuff for the ImportDialog, the memory sets itself to NULL! It happens HERE:

bool GetCheckBoxState(HWND hw,int Control)
{	
	bool b;
	HWND C;
	C=GetDlgItem(hw,Control);
	NumToBool(SendMessage(C,BM_GETCHECK,0,0),&b); //<----- From HERE
	return b;
}

void NumToBool(int num,bool* Target) //<---- To HERE
{
	if(num<=0)
	{
		*Target=false;
	}else
	{
		*Target=true;
	}
}

The debugger jumps to the other function and the data of my buffer, somewhere in the memory, is 0. Why??? EDIT: Oh! And you might have to know, that the buffer that holds the Data is private...
Advertisement
Well, I guess I should point out the obvious, that memory is not "setting itself to null" :) You've got a bug in your program, and somewhere you're probably accidentally overwriting it.

First of all, please post the code that actually allocates the memory from the other class.

Second, how to find the problem? First, put a breakpoint on the line of code that allocates the memory. Step over this line of code. In order to find out when Data stops referring to the memory you're interested in and starts referring to junk we'll use a hardware breakpoint (which is not the same as a breakpoint you're accustomed to). Open the watch window and enter "&Data" (actually byte* Data = ... declares a local variable which will go out of scope. I assume you've actually stored this into a class member variable, in which case you'll want to replace 'Data' with the name of the class member variable). Anyway, this will display some address. Open the Breakpoints window in Visual Studio and there's a button that says "New Breakpoint". From the drop down choose "Data Breakpoint" and enter the address displayed in the watch window from the previous step.

So, why did we use &Data instead of Data in the Data Breakpoints window? Well, as soon as you execute the line "byte* Data = new byte[512]", data points to a valid memory address. Or in other words, the value of Data (which is a pointer) is some address which refers to the memory you're interested in.

What you want is to find out exactly when this VALUE changes to a different value. Since the value stored in the pointer also lives at some address, you want to find out when something modifies the value at *that* address. That address is given by &Data.


Note that if you do all this and then stop debugging and restart, you'll have to reset your data breakpoint since the address will probably change on each run.

Basically what this will give you is that the instant Data is written to NULL, your program will break and give you a callstack so you can see what line of code is causing the memory to be overwritten.
Ah!

I forgot to memcpy from a local variable.
But thanks anyways! I wouldn't have looked at this piece of code,
if you hadn't told me to do :)

This topic is closed to new replies.

Advertisement