Memory leak (vector, methinks)

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

Join Date: Sep 2007
Posts: 35
Reputation: Evan M is an unknown quantity at this point 
Solved Threads: 4
Evan M's Avatar
Evan M Evan M is offline Offline
Light Poster

Memory leak (vector, methinks)

 
0
  #1
Mar 10th, 2009
I'm having several difficulties with memory leaks in a program (I've been using "Visual Leak Detector" to check for memory leaks). I've simplified the code to the following:

  1. #include "vld.h"
  2. #include "vldapi.h"
  3. #include <string>
  4. #include <vector>
  5.  
  6. class BaseFoo {
  7. public:
  8. BaseFoo() {}
  9. ~BaseFoo() {}
  10. };
  11.  
  12. class ButtonFoo {
  13. public:
  14. void setText( std::string text ) { _text = text; }
  15.  
  16. private:
  17. std::string _text;
  18. };
  19.  
  20. class InheritedFoo : public BaseFoo {
  21. public:
  22. InheritedFoo();
  23.  
  24. private:
  25. ButtonFoo buttons[4];
  26. };
  27.  
  28. InheritedFoo::InheritedFoo()
  29. {
  30. ButtonFoo button;
  31.  
  32. for( unsigned int i = 0; i < 4; i++ ) {
  33. buttons[i] = button;
  34. }
  35.  
  36. buttons[0].setText( "NO MORE MEMORY LEAKS" );
  37. buttons[1].setText( "I DONT LIKE YOU ANYMORE" );
  38. buttons[2].setText( "I DONT WANT YOU ANYMORE" );
  39. buttons[3].setText( "GO AWAY" );
  40. }
  41.  
  42. int main( int argc, char **argv )
  43. {
  44. std::vector<BaseFoo*> modules;
  45.  
  46. modules.push_back( new InheritedFoo() );
  47.  
  48. for( unsigned int i = 0; i < modules.size(); ++i ) {
  49. delete modules[i];
  50. }
  51. }

I suspect 'modules' is the source of the memory leak. Why is the vector leaking memory? And is there a way around it?
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Memory leak (vector, methinks)

 
0
  #2
Mar 10th, 2009
Your loop, where you have buttons[i] = button... yeah, that doesn't create new instances of ButtonFoo. You are taking button[0] and pointing it to button. Then taking button[1] and pointing it to the same object as button[0]....
Last edited by Comatose; Mar 10th, 2009 at 7:54 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 978
Reputation: mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice mitrmkar is just really nice 
Solved Threads: 208
mitrmkar mitrmkar is offline Offline
Posting Shark

Re: Memory leak (vector, methinks)

 
1
  #3
Mar 10th, 2009
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: Memory leak (vector, methinks)

 
1
  #4
Mar 10th, 2009
A classical case. Only base class destructor called because your BaseFoo is not a polymorphic class. It can't deallocate any vectors in the derived class: it knows nothing about its descendants.
Make it as a polymorphic class (fortunately, it's so simple) and see what happens:
  1. class BaseFoo {
  2. public:
  3. BaseFoo() {}
  4. virtual ~BaseFoo() {}
  5. };
Last edited by ArkM; Mar 10th, 2009 at 8:16 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 35
Reputation: Evan M is an unknown quantity at this point 
Solved Threads: 4
Evan M's Avatar
Evan M Evan M is offline Offline
Light Poster

Re: Memory leak (vector, methinks)

 
0
  #5
Mar 10th, 2009
Thanks for the help. I always assumed since the constructor isn't virtual, the destructor wouldn't have to be either. The memory leaks are gone
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC