954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C++ difference between memory management in Debug vs. Release: data loss problem

Hello,

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

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

	textBox(string text) {
		m_text = text;
	}

	string	getText()	{ return m_text; }

private:
	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?

Thanks!

KolosoK
Newbie Poster
6 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

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.

KolosoK
Newbie Poster
6 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

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


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

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Memory can be trashed in many ways.

Edit: 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..

hllsen
Newbie Poster
1 post since Feb 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You