Re: Compiling a C++/Wx program

  • From: Jamal Mazrui <empower@xxxxxxxxx>
  • To: programmingblind@xxxxxxxxxxxxx
  • Date: Wed, 28 Nov 2007 12:36:52 -0500 (EST)

Hi Marlon,
This fruit basket program is my first nontrivial program in C++, so I'm
not surprised the code has flaws, and hope that you and other C++
developers on the list will help improve it so people can benefit from
more fruit basket demos in C++ (in addition to MFC).

Regarding your specific points, I'm not sure they are correct in this
case.  Below I'm pasting Wx documentation about garbage collection.  It
appears that destroying the dialog object, which the code does, will
take care of its child controls as well.  Other application clean-up is
done by Wx when the main loop ends.

Concerning the labels for the edit box and listbox, they do not need
variable references because the constructor includes the parent dialog
as a parameter, and unlike the other controls, the labels are not used
by the code after creation.

If I'm missing anything else, please let me know and suggest syntax to
correct the problems.  I now have a working program which I'll post
soon.

Cheers,
Jamal

Allocating and deleting wxWidgets objects
In general, classes derived from wxWindow must dynamically allocated
with new
and deleted with delete. If you delete a window, all of its children and
descendants will be automatically deleted, so you don't need to delete
these
descendants explicitly.

When deleting a frame or dialog, use Destroy rather than delete so that
the
wxWidgets delayed deletion can take effect. This waits until idle time
(when
all messages have been processed) to actually delete the window, to
avoid
problems associated with the GUI sending events to deleted windows.

Don't create a window on the stack, because this will interfere with
delayed
deletion.

If you decide to allocate a C++ array of objects (such as wxBitmap) that
may
be cleaned up by wxWidgets, make sure you delete the array explicitly
before
wxWidgets has a chance to do so on exit, since calling delete on array
members
will cause memory problems.

wxColour can be created statically: it is not automatically cleaned up
and is
unlikely to be shared between other objects; it is lightweight enough
for
copies to be made.

Beware of deleting objects such as a wxPen or wxBitmap if they are still
in
use. Windows is particularly sensitive to this: so make sure you make
calls
like wxDC::SetPen(wxNullPen) or wxDC::SelectObject(wxNullBitmap) before
deleting a drawing object that may be in use. Code that doesn't do this
will
probably work fine on some platforms, and then fail under Windows.


On
Wed, 28 Nov 2007, Marlon
Brandão de Sousa wrote:

> Date: Wed, 28 Nov 2007 10:35:44 -0200
> From: Marlon Brandão de Sousa <splyt.lists@xxxxxxxxx>
> Reply-To: programmingblind@xxxxxxxxxxxxx
> To: programmingblind@xxxxxxxxxxxxx
> Subject: Re: Compiling a C++/Wx program
>
> Hello Jamal,
> please don't take this as a criticism, but I have seen you create
> several components on the heap but don't get then collected. In c++
> everytime you use a new alocation you should delete the components,
> otherwise you'll have memory leaks. You have used also this line:
>
> new wxStaticText(dlg, wxID_ANY, "&Fruit:", wxPoint(14, 14), wxDefaultSize);
>
> which creates an object, but you didn't assign a pointer to it, so it
> would be of no use and will not be able to be deleted because you
> didn't stored its address in a pointer.
> While I don't understand completely the wxwidgets framework, I don't
> think this code would be a good fruit basket sample because one have
> to understand from the beginning that if they want to use c++ they
> will have to take the responsability of deleting every object they
> alocate in the memory.
> Thanks
> Marlon
>
> 2007/11/27, Jamal Mazrui <empower@xxxxxxxxx>:
> > Jaffar's tutorial helped me build the Wx libraries with Microsoft Visual
> > C++ 2005 Express.
> --
> When you say "I wrote a program that crashed Windows," people just
> stare at you blankly and say "Hey, I got those with the system, for
> free."
> Linus Torvalds
> __________
> View the list's information and change your settings at
> //www.freelists.org/list/programmingblind
>
__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind

Other related posts: