Začínám se učit s WxWdigets v prostředí C++ a mám trochu problém s neuvolňovanou pamětí. Použiju-li třeba ukázkový příklad hello world ze stránky http://www.wxwidgets.org/docs/tutorials/hello.htm, tak se program sice zkompiluje a funguje, ale valgrind hází tuny memory leaku.
Našel jsem článek http://wiki.wxwidgets.org/Avoiding_Memory_Leaks a pokusil se do programu zapracovat metodu Destroy(), který by měla uvolnit alokovanou paměť, ale nic se nezměnilo.
Zde je můj kód.
/*
* hworld.cpp~
*/
#include "wx/wx.h"
#include <iostream>
using namespace std;
class MyApp: public wxApp
{
virtual bool OnInit();
};
class MyFrame: public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
void OnQuit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnClose(wxCloseEvent& event);
DECLARE_EVENT_TABLE()
};
enum
{
ID_Quit = 1,
ID_About,
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Quit, MyFrame::OnQuit)
EVT_MENU(ID_About, MyFrame::OnAbout)
EVT_CLOSE(MyFrame::OnClose)
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
void MyFrame::OnClose(wxCloseEvent& event)
{
cout << "Nicim okno... ." << endl;
this->Destroy();
}
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame( _("Hello World"), wxPoint(50, 50),
wxSize(450,340) );
frame->Show(true);
SetTopWindow(frame);
return true;
}
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame( NULL, -1, title, pos, size )
{
wxMenu *menuFile = new wxMenu;
menuFile->Append( ID_About, _("&About...") );
menuFile->AppendSeparator();
menuFile->Append( ID_Quit, _("E&xit") );
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append( menuFile, _("&File") );
SetMenuBar( menuBar );
CreateStatusBar();
SetStatusText( _("Welcome to wxWidgets!") );
}
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
{
Close(TRUE);
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
wxMessageBox( _("This is a wxWidgets Hello world sample"),
_("About Hello World"),
wxOK | wxICON_INFORMATION, this);
}
Do kodu jsem přidat volání funkce Destroy(), ale stejně, pustím-li program přes valgrind, dostanu následující výpis:
==3233== Memcheck, a memory error detector
==3233== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==3233== Using Valgrind-3.6.0.SVN-Debian and LibVEX; rerun with -h for copyright info
==3233== Command: ./a.out
==3233==
Nicim okno... .
==3233==
==3233== HEAP SUMMARY:
==3233== in use at exit: 1,119,262 bytes in 7,922 blocks
==3233== total heap usage: 26,643 allocs, 18,721 frees, 3,892,154 bytes allocated
==3233==
==3233== LEAK SUMMARY:
==3233== definitely lost: 2,620 bytes in 6 blocks
==3233== indirectly lost: 6,384 bytes in 201 blocks
==3233== possibly lost: 566,015 bytes in 3,351 blocks
==3233== still reachable: 544,243 bytes in 4,364 blocks
==3233== suppressed: 0 bytes in 0 blocks
==3233== Rerun with --leak-check=full to see details of leaked memory
==3233==
==3233== For counts of detected and suppressed errors, rerun with: -v
==3233== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 9 from 8)
Jak se tomu vyhnout?
Děkuji, Jakub