| | |
Can't prevent memory leak
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: May 2007
Posts: 3
Reputation:
Solved Threads: 0
Hi!
I hope you could help me with this one. I simplified the code of my program to its basic problem, and the problem is that I do not seem to understand why this program does not release its memory. I check it at the moments of the
I claim 1000 array's of 1000 items, containing int's. That consumes 4M, and that is what the System Monitor indicates. But when I try to free memory, this does not seem to help. Why? Is it because I do not free memory appropriately, or is the used space never claimed back by the operating system?
Thanks in advance for your help!
Bart.
I hope you could help me with this one. I simplified the code of my program to its basic problem, and the problem is that I do not seem to understand why this program does not release its memory. I check it at the moments of the
getline's, using Ubuntu's System Monitor.I claim 1000 array's of 1000 items, containing int's. That consumes 4M, and that is what the System Monitor indicates. But when I try to free memory, this does not seem to help. Why? Is it because I do not free memory appropriately, or is the used space never claimed back by the operating system?
Thanks in advance for your help!
Bart.
C++ Syntax (Toggle Plain Text)
class C { public: int *bla; C() {bla = new int[1000];} ~C() {delete[] bla;} }; int main(int argc, char *argv[]) { string t; set<C*>* pSet; set<C*>::iterator iter; pSet = new set<C*>; getline(cin, t); for (int i=0; i<1000; i++) { pSet->insert(new C()); } getline(cin, t); for (iter=pSet->begin(); iter!=pSet->end(); ++iter) { delete *iter; } getline(cin, t); pSet->clear(); getline(cin, t); delete pSet; getline(cin, t); return 0; }
Last edited by bcramer; May 17th, 2007 at 12:01 pm.
The memory is freed, it's just not given back to the OS.
This is so you can allocate and free memory through the life of the program without going to the hassle of asking the OS each time you want some more memory.
If you repeat the allocation, then your system monitor should show no change. If it does keep going up with each new allocation, and you think you are freeing memory, then you may have a leak.
When you finally quit the program, then all will be well.
This is so you can allocate and free memory through the life of the program without going to the hassle of asking the OS each time you want some more memory.
If you repeat the allocation, then your system monitor should show no change. If it does keep going up with each new allocation, and you think you are freeing memory, then you may have a leak.
When you finally quit the program, then all will be well.
![]() |
Similar Threads
- can't find my memory leak (C++)
- Pointers (C++)
- changing the way Internet Explorer. (HTML and CSS)
- Memory Leak? (C)
- Constructor and Convertion operator are the same,how to avoid memory leak? (C++)
Other Threads in the C++ Forum
- Previous Thread: Linker -help needed
- Next Thread: Need some assistance on Homework PLEASE
Views: 1418 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






