Re: Memory allocation troubles



Olaf FrÄczyk wrote:

I write an application that requires much memory to run.
It is impossible to assume that every time I ask for memory I'll get it.
The application creates many threads (up to several hundreds) each
thread taking about 0,6 MB.
And when I am out of memory I could just wait for some threads to
finish, and try again.
Aborting program is not an option, because before dying I need to clean
things up.

As I understand, when I create a widget, add a timer etc. I allocate
some memory. But in the API description there is nothing said that on
return I should get NULL value or something.

In other place I have found, that the glib library just aborts the
program if it is unable to allocate memory. As gtk is build on top of
glib, should I assume that there is no out of memory handling in gtk
also?

If reliable memory management in gtk is impossible, does anyone know
another toolkit that meet this requirement?

Oh, I just remembered that you can use your own malloc() implementation
with Glib/GTK+!  So, while this doesn't solve your problem out of the box,
you can do something like this:

 char *
 almost_safe_malloc (int num_bytes)
 {
   char *memory_block = malloc (num_bytes);

   if (memory_block == NULL) {
     do_aggressive_resource_freeing ();
     memory_block = malloc (num_bytes);

     while (memory_block == NULL) {
       /* Nasty.  May be completely inappropriate for some threads. */
       sleep (1);
       memory_block = malloc (num_bytes);
     }
   }

   return memory_block;
 }

See g_mem_set_vtable().

Paul




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