Curious about design choices

Robin Dunn robin at alldunn.com
Mon Aug 6 10:53:44 PDT 2007


Nathan Ridge wrote:
> I'm a new user of wxWidgets and I'm curious about a couple of design 
> choices in wxWidgets:
>  
> 1) What is the rationale for imposing the restriction that certain 
> objects (such as those derived from wxWindow) cannot be created on the 
> stack? (I'm guessing that overloading operators new/delete may be 
> involved, but I don't understand why that's necessary.)

Because the window belongs to its parent, not to you.  Since the parent 
will always use delete on the child then it must be allocated on the 
heap.  The exception to this rule is wxDialogs.  You can put them on the 
stack if you want to, and if you don't then you need to explicitly call 
Destroy yourself because they don't Destroy themselves like frames do.


>  
> 2) What is the rationale for having to use Destroy() rather than 
> "delete" to destroy an object created using "new"? (I've been told that 
> Destroy() waits for all events sent to the object to be processed and 
> then deletes the object, but I'm wondering why code that does that can't 
> be placed in the destructor?)

Because by the time the destructor runs it is too late to wait for 
anything, the object is already in the process of being destroyed.  BTW, 
currently the waiting is only done for top-level windows.  Others are 
deleted immediately when Destroy is called, but you probably shouldn't 
depend on it being either one way or the other if possible since that 
could be considered an implementation detail.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wx-users mailing list