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:
#include "vld.h"
#include "vldapi.h"
#include <string>
#include <vector>
class BaseFoo {
public:
BaseFoo() {}
~BaseFoo() {}
};
class ButtonFoo {
public:
void setText( std::string text ) { _text = text; }
private:
std::string _text;
};
class InheritedFoo : public BaseFoo {
public:
InheritedFoo();
private:
ButtonFoo buttons[4];
};
InheritedFoo::InheritedFoo()
{
ButtonFoo button;
for( unsigned int i = 0; i < 4; i++ ) {
buttons[i] = button;
}
buttons[0].setText( "NO MORE MEMORY LEAKS" );
buttons[1].setText( "I DONT LIKE YOU ANYMORE" );
buttons[2].setText( "I DONT WANT YOU ANYMORE" );
buttons[3].setText( "GO AWAY" );
}
int main( int argc, char **argv )
{
std::vector<BaseFoo*> modules;
modules.push_back( new InheritedFoo() );
for( unsigned int i = 0; i < modules.size(); ++i ) {
delete modules[i];
}
}
I suspect 'modules' is the source of the memory leak. Why is the vector leaking memory? And is there a way around it?