wxWidgets and memory allocation failures.

Dave Bee davebeeus at yahoo.com
Sun Apr 22 22:26:27 PDT 2007


Wow! after taking some break from wxWidgets I just
discovered this thread and I am pretty much amazed
that wxWidgets would not even _hint_ to the programmer
that a memory allocation has failed.

Is this for real?

I don't mean to be sounding critical or something, but
I am trying to understand how such a wonderful library
goes against something so fundamental that I learned
in school and in the industry. 

I can be OK with the fact that wxWidgets doesn't throw
exceptions. malloc() doesn't do that either.

But not even returning NULL for a failed 'new'?

Could someone please confirm my understanding that as
a wxWidgets programmer I should not even bother
checking for NULL the return value from 'new'?

BTW, I am well aware of the fact that unlike 'C', C++
is not supposed to return NULL on failed allocations,
but rather throw an exception. (or have I got that
wrong, too?)

Thanks,
Dave


--- Jurko Gospodnetiæ <jurko_for_boost at mail.inet.hr>
wrote:

>    Hi.
> 
> >  Unfortunately it's not a simple problem and I
> don't have any satisfactory
> > answer to it. We could call some user-defined
> function when memory
> > allocation fails and wash our hands of the issue
> by saying that we allow
> > the programmer to do whatever is most appropriate
> in each particular case.
> > But this wouldn't be really helpful neither. And I
> don't know what would.
> 
>    Thinking about this some more, there is not much
> freedom available in 
> handling this case. It is definitely an error, and
> an error that should 
> not cause 'undefined behavior'. As an error it may
> be handled by either:
>    1. terminating the application
>    2. returning an error code (either
> global/thread-local variable, 
> return value or an error-code parameter).
>    3. throwing an exception.
> 
>    Before this handling wxWidgets could call an
> application defined 
> function, possibly allowing the application callback
> to free up some 
> memory and have wxWidgets retry the previously
> failed memory allocation 
> instead of simply failing.
> 
>    Since wxWidgets does not use exceptions option 3
> is out.
> 
>    Returning an error code would most likely (and I
> am only making an 
> educated guess here) have an enormous impact on the
> current interface 
> and implementation. For example, you would no longer
> be able to to 
> simple operations like string appending using
> chained operator<<() calls 
> if they remain implemented in the same way as they
> are now. So that 
> takes option 2 most likely out of the picture as
> well.
> 
>    And all you are left with is option 1 (with
> possibly an application 
> defined function call as described above).
> 
>    Hope this helps..
> 


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 




More information about the wx-users mailing list