I have started playing around with wxWidgets using c++. I have made a simple hello world application following along with tutorials.
Out of curiosity I ran the complied program through valgrind and had the following result.
==92057== LEAK SUMMARY:
==92057== definitely lost: 91,768 bytes in 292 blocks
==92057== indirectly lost: 119,108 bytes in 4,828 blocks
==92057== possibly lost: 9,238 bytes in 143 blocks
==92057== still reachable: 4,590,951 bytes in 43,100 blocks
==92057== of which reachable via heuristic:
==92057== length64 : 13,360 bytes in 199 blocks
==92057== newarray : 2,528 bytes in 78 blocks
==92057== suppressed: 0 bytes in 0 blocks
should this output be trusted? Is there a standard way to free memory associated with wxWidgets?
For example:
wxButton *clear = new wxButton(panel, ID_RESET, wxT("Clear"));
should this be deleted in the destructor for its parent object? I am not doing any memory allocations in this project though my own code. Just displaying a window with some controls on via wxWidgets.
I presume that you are doing it on Linux?
All those are false positives or leaks coming from system libraries.
In terms of wxWidgets you don't need to delete memory allocated for wx object. The library internally works like a smart pointer.
When you create a main frame it will be deleted when the application object will be destroyed. When you create a panel as parent of the main frame it will be automatically deleted when the main frame will be destroyed which will happen when the application object will be destroyed.
And so forth - just like in the children's poem This is the house that Jack built
. You don't have to do anything in the destructor.
However if you allocate memory for any non-wx object, you absolutely have to release it (call delete
on the pointer).
[EDIT]
Very important!!
Also, please understand - the very important part - all those controls have to have a parent window as a first parameter to constructor.
Also, when you try to create a modal dialog, that dialog may not have a parent (pass NULL). In this case - DO NOT create it on the heap!! Create it on the stack and when the variable goes out of scope the dialog object will be destroyed. With the same concept of deleting all children automatically.
This is the only exception of heap vs stack creation in wxWidgets.
[/EDIT]