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

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 6
Reputation: KolosoK is an unknown quantity at this point 
Solved Threads: 0
KolosoK KolosoK is offline Offline
Newbie Poster

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

 
0
  #1
Aug 11th, 2009
Hello,

I have a weird problem. I have a class implemented that looks something like the following:
  1. class textBox {
  2. public:
  3. // default constructor
  4. textBox() {
  5. m_text = "";
  6. }
  7.  
  8. textBox(string text) {
  9. m_text = text;
  10. }
  11.  
  12. string getText() { return m_text; }
  13.  
  14. private:
  15. string m_text;
  16. };

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 -
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #2
Aug 11th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 6
Reputation: KolosoK is an unknown quantity at this point 
Solved Threads: 0
KolosoK KolosoK is offline Offline
Newbie Poster

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

 
0
  #3
Aug 11th, 2009
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.
Last edited by KolosoK; Aug 11th, 2009 at 6:07 pm.
- KolosoK -
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

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

 
0
  #4
Aug 11th, 2009
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
  1. int *p = new int [10];
  2. delete [] p;
  3. // more code
  4. delete [] p; // oops

Freeing what wasn't allocated
  1. int *p = new int [10];
  2. // code
  3. p++; // say walking the array
  4. delete [] p; // oops, this isn't what new returned

Edit:
I wrote this, then saw your fix, so I posted it anyway.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC