wxWidgets and memory allocation failures.

Jurko Gospodnetić jurko_for_boost at mail.inet.hr
Sat Apr 7 14:27:15 PDT 2007


   Hi.

> And, of course, some popular platforms (such as Linux) never throw 
> bad_alloc anyhow and just terminate any program which runs out of memory
> forcibly.

   Hmmm, I did not know this was the case with Linux. Are you absolutely 
certain that Linux's OS functions for dynamically allocating memory 
forcibly terminate the program in case of failure? With no chance to 
attempt to do something about it on the application side? Sounds 
strange, but on the other hand, I have not yet had a chance do to any 
serious programming on Linux.


>  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.

   I think if you do not want to throw exceptions then calling an 
application defined function would be the only possible solution that 
would allow the application to attempt to free up some memory and 
terminate as safely as possible, as suggested by Andreas Micheler in 
another reply.

   If nothing else, it would allow it to terminate without causing data 
corruption such as happens when you fail to allocate memory for storing 
data inside a wxString object.

   A slightly harsher solution would be to terminate the process 
directly which I think would also be superior to the current implementation.


>  Yes, if memory is exhausted, wxWidgets will crash.

   :-((( heh... actually I think it cause something worse than 'a 
crash'. One can expect applications to be written so that they do not 
corrupt their data in case they get terminated forcibly (power 
failure?), but there is no way to make applications safe if wxWidgets 
starts writing over memory not allocated to its objects. In the current 
implementation the best you can hope for is an application crash due to 
an access violation, however worse things can happen...

   Best regards,
     Jurko Gospodnetić





More information about the wx-users mailing list