943,923 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1561
  • C++ RSS
Mar 10th, 2009
0

Memory leak (vector, methinks)

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

C++ Syntax (Toggle Plain Text)
  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?
Reputation Points: 11
Solved Threads: 5
Light Poster
Evan M is offline Offline
42 posts
since Sep 2007
Mar 10th, 2009
0

Re: Memory leak (vector, methinks)

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Mar 10th, 2009
1

Re: Memory leak (vector, methinks)

Reputation Points: 1105
Solved Threads: 389
Posting Virtuoso
mitrmkar is offline Offline
1,714 posts
since Nov 2007
Mar 10th, 2009
1

Re: Memory leak (vector, methinks)

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:
c++ Syntax (Toggle Plain Text)
  1. class BaseFoo {
  2. public:
  3. BaseFoo() {}
  4. virtual ~BaseFoo() {}
  5. };
Last edited by ArkM; Mar 10th, 2009 at 8:16 pm.
Reputation Points: 1234
Solved Threads: 347
Postaholic
ArkM is offline Offline
2,001 posts
since Jul 2008
Mar 10th, 2009
0

Re: Memory leak (vector, methinks)

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
Reputation Points: 11
Solved Threads: 5
Light Poster
Evan M is offline Offline
42 posts
since Sep 2007

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: C++ and Polymorphism
Next Thread in C++ Forum Timeline: variable trouble?





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


Follow us on Twitter


© 2011 DaniWeb® LLC