I have a weird problem. I have a class implemented that looks something like the following:

class textBox {
	// default constructor
	textBox()  {
		m_text = "";

	textBox(string text) {
		m_text = text;

	string	getText()	{ return m_text; }

	string	m_text;

Now, when I use getText() in Debug mode (compiling using VS2008), it always returns the correct string that was placed into m_text via the constructor. However, when I use getText() in Release mode, the first time it returns the correct string, but from that point on, some (starting at the beginning) or all of the data in the string becomes corrupt or missing. I'm pretty sure that this is a problem in how memory is managed in Debug vs Release building. How can I make sure that the memory won't get overwritten with garbage in Release mode without dynamically allocating the string?


7 Years
Discussion Span
Last Post by hllsen

It's a pretty sure thing that you're looking at the effect rather than the cause.

Something else is probably trashing memory. Here is just where you first notice there is a problem.

In a correct program, there is no apparent difference between debug and release (except speed). That you are observing a difference is a sure sign of bugs.

It depends on how large the program is as to what to do about it.

One thing to do is examine carefully all your other classes. Especially any where you call new/delete explicitly.


I've made sure that my classes' constructors initialize all of the present variables to either NULL, 0, or whatever is passed in through the constructor. The program is about 3000 lines of code long, so yes, I haven't ruled out bugs yet.

What are some examples of memory being written past the space allocated for it?

EDIT: Problem solved. I had a class being cast as something it shouldn't, which messed with the memory. Woops.


Memory can be trashed in many ways.

Running off the end of the array

int length = 10;
int *p = new int[length];
for ( int i = 0 ; i <= length ; i++ ) p[i] = 0;

<= should be <

Freeing the same memory twice

int *p = new int [10];
delete [] p;
// more code
delete [] p; // oops

Freeing what wasn't allocated

int *p = new int [10];
// code
p++;  // say walking the array
delete [] p;  // oops, this isn't what new returned

I wrote this, then saw your fix, so I posted it anyway.


Memory can be trashed in many ways.

I wrote this, then saw your fix, so I posted it anyway.

i'm glad that you wrote them "anyway".. because you save my life, thank you and the others..

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.