Can't prevent memory leak

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2007
Posts: 3
Reputation: bcramer is an unknown quantity at this point 
Solved Threads: 0
bcramer bcramer is offline Offline
Newbie Poster

Can't prevent memory leak

 
0
  #1
May 17th, 2007
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 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.


  1. class C
  2. {
  3. public:
  4.  
  5. int *bla;
  6. C() {bla = new int[1000];}
  7. ~C() {delete[] bla;}
  8. };
  9.  
  10. int main(int argc, char *argv[])
  11. {
  12. string t;
  13. set<C*>* pSet;
  14. set<C*>::iterator iter;
  15.  
  16. pSet = new set<C*>;
  17.  
  18. getline(cin, t);
  19. for (int i=0; i<1000; i++)
  20. {
  21. pSet->insert(new C());
  22. }
  23. getline(cin, t);
  24. for (iter=pSet->begin(); iter!=pSet->end(); ++iter)
  25. {
  26. delete *iter;
  27. }
  28. getline(cin, t);
  29. pSet->clear();
  30. getline(cin, t);
  31. delete pSet;
  32. getline(cin, t);
  33.  
  34. return 0;
  35. }
Last edited by bcramer; May 17th, 2007 at 12:01 pm.
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Can't prevent memory leak

 
0
  #2
May 17th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 3
Reputation: bcramer is an unknown quantity at this point 
Solved Threads: 0
bcramer bcramer is offline Offline
Newbie Poster

Re: Can't prevent memory leak

 
0
  #3
May 23rd, 2007
Hi Salem,

thanks for your answer. Indeed, this is the solution in this program. Seems that I have to look through my original program now, and search for the bug...
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: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Can't prevent memory leak

 
0
  #4
May 23rd, 2007
Consider using valgrind (since you're using some kind of Linux) to help you track down the memory leaks.

Though you might want to practice with small programs like your example, with carefully introduced deliberate mistakes to get used to the kind of things it tells you.
Reply With Quote Quick reply to this message  
Reply

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




Views: 1418 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC