Re: out-of-mem handler

From: Sebastian Wilhelmi <>
>Hi Havoc,
>> I remember that we talked about how to do an out-of-memory handler for
>> g_malloc() etc., did anyone ever do the patch? Should I make a patch?
>Just to reiterate:
>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...

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

> [oups... just found the del key]
>So, please don't add an out-of-mem-callback.
>Sebastian Wilhelmi                   |            här ovanför alla molnen
>           |     är himmlen så förunderligt blå
>   |



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