| | |
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:
Solved Threads: 0
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:
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!
I have a weird problem. I have a class implemented that looks something like the following:
C++ Syntax (Toggle Plain Text)
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 -
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.
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.
•
•
Join Date: Jun 2009
Posts: 6
Reputation:
Solved Threads: 0
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.
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 -
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
<= should be <
Freeing the same memory twice
Freeing what wasn't allocated
Edit:
I wrote this, then saw your fix, so I posted it anyway.
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;Freeing the same memory twice
C++ Syntax (Toggle Plain Text)
int *p = new int [10]; delete [] p; // more code delete [] p; // oops
Freeing what wasn't allocated
C++ Syntax (Toggle Plain Text)
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.
![]() |
Similar Threads
- Unfencing memory-- (C++)
- Memory management - Advice please (C++)
- memory management in wndows 2000 (Windows NT / 2000 / XP)
- Unix Memory Management. (*nix Hardware Configuration)
- difference between memory w/black chips on one side and both sides? (Motherboards, CPUs and RAM)
- Help with efficient memory management (C)
Other Threads in the C++ Forum
- Previous Thread: Need some help with using the ios_base class. It won't output correctly
- Next Thread: Derived class
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






