944,137 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1907
  • C++ RSS
May 17th, 2007
0

Can't prevent memory leak

Expand Post »
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.


C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bcramer is offline Offline
3 posts
since May 2007
May 17th, 2007
0

Re: Can't prevent memory leak

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
May 23rd, 2007
0

Re: Can't prevent memory leak

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bcramer is offline Offline
3 posts
since May 2007
May 23rd, 2007
0

Re: Can't prevent memory leak

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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Linker -help needed
Next Thread in C++ Forum Timeline: Need some assistance on Homework PLEASE





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC