Re: [gtk-list] Re: g_malloc() aborts when enough memory is notfound?



On Tue, 2 Nov 1999, Emmanuel DELOGET wrote:

> > Mikko Rantalainen <mira@st.jyu.fi> writes:
> > [snipped - sorry]
> >
> > If you need non-aborting behavior, use malloc() ;-)
> 
>     Hi Owen,
> 
>     I don't know if this is a good idea. If it is possible
>     to extend the callback architecture to glib (for version
>     1.30, perhaps :)), the abort behavior should be avoided in
>     some cases. Consider the following (in a multiple window
>     text processor)
> 
>     enum
>     {
>         GALLOC_WAIT,        /* do not malloc, return NULL on g_malloc()
> error */
>         GALLOC_NOWAIT,  /* try to alloc again */
>         GALLOC_ERROR      /* cannot do anything, so g_malloc() aborts */
>     };

g_malloc() is used in a *lot* of places (usually *everywhere* in gtk/gnome
programs where dynamic allocations are required). most (i.e. 99.999%) of
these places do *not* check for NULL because g_malloc() is documented
to never return NULL.
so a global flag is probably *not* appropriate here.

>     /* during init */
>     guint granularity = 64 * 1024 ; /* ask for 64ko on a malloc error */
>     gallocator_init(my_callback_for_the_no_more_memory_error, granularity);
> 
>     /* lots of g_malloc and then...*/
> 
>     gint my_callback_..._error(guint requested_size)
>     {
>         if (number_of_opened_docs > 1)
>         {
>             show_simple_dialog("no more memory : you should close some
> documents");
>             return (GALLOC_WAIT);
>         }
>         else
>         {
>             show_simple_dialog("fatal : no more memory");
>             return (GALLOC_ERROR);
>         }
>     }
> 
>     Does not seems to be bad for me :)

out of curiosity, how do you popup a dialog if there's no memory available?

> 
>     Emmanuel
> 

---
ciaoTJ



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]