Search code examples
c++memory-leakswxwidgets

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.


Solution

  • 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
    }