Re: out-of-mem handler



Hi Emmanuel,

> > Do not add a callback for out of memory. You wouldn't be allowed to
> > call any glib-function (let alone GTK) inside this handler, as that
> > might screw up the internal state, depending on where the g_malloc
> > call came from, also Xlib doesn't allow overloading the Xmalloc. Thus
> > the callback wouldn't be called, when out of mem in X.
> 
>     Sure the callback would not help when the programmer tries to
>     allocate
>     a few bytes. But it should be interesting to have it when trying
>     to allocate large chunks of memory (in order to store a big
>     pixmap for example). Then allocating only a few bytes to tell
>     the user that a malloc failed should not hurt..

But if the calling program expects being told, that there is not enough
memory, this is better done by returning NULL.
 
> >Add a g_malloc_try (we already talked about it then). This is a really
> >necessary function, but a handler will yield exactly nothing.
> 
>     The g_malloc_try will lead exactly to this callback mecanism
>     but without any callback. When the programmer wants to use such a
>     functions, he does:
> 
> void *my_malloc(size_t size)
> {
>     void *ptr;
> 
>     ptr = g_malloc_try(size);
>     if (ptr == NULL)
>     {
>         print_error_message_and_exit_if_needed();
>     }
>     return (ptr);
> }
> 
>     It's just the same as having the print_error function
>     called as a callback when the g_malloc failed, except
>     that you continue to call the g_malloc() func instead
>     of having another function to call when trying to alloc
>     a chunk of memory.

Yes, but here the programmer is doing something bad, we can't prevent that,
but we shouldn't make things, that can almost only be used in a bad and broken
way, part of the glib API.

Bye,
Sebastian
-- 
Sebastian Wilhelmi                   |            här ovanför alla molnen
mailto:wilhelmi@ira.uka.de           |     är himmlen så förunderligt blå
http://goethe.ira.uka.de/~wilhelmi   |



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