Vinayak Garg
Vinayak Garg

Reputation: 6606

Memory leakage due to SetBackgroundBitmap

I have added a image (tiled) as a background to a class inheriting from wxPanel

Inside the constructor, the second line below is causing memory leakage, (reported in debug mode)

wxImage bg(_("images/textures/icobbg8.jpg"), wxBITMAP_TYPE_JPEG);

SetBackgroundBitmap(wxBitmap(bg));

If i comment the SetBackgroundBitmap memory leak is no longer reported. Note - During debugging, and after viewing call stack i rounded on this statement.

Please tell me, how to overcome memory leak.

Upvotes: 0

Views: 183

Answers (2)

daruzhang
daruzhang

Reputation: 26

Your should call SetBackgroundBitmap(wxNullBitmap) in your destructor

Class MyPanel:public wxPanel
{
   MyPanel(wxWindow* parent, int x, int y, int w, int h);
   ~MyPanel();
};

MyPanel::~MyPanel()
{
   SetBackgroundBitmap(wxNullBitmap); //set null bitmap backgrond, so not 
                                      //reference bg to overcome the leak
}

Upvotes: 1

ravenspoint
ravenspoint

Reputation: 20457

When the constructor exits, the wxImage bg will be destroyed. However, the class still exists and the background image is still needed.

Try changing the bg from a local to an attribute of the class.

Upvotes: 1

Related Questions