Re: [gtk-list] Bug with GMemChunk



On 13 Aug 1999, Soeren Sandmann wrote:

> 
> This piece of code fails with a segmentation fault on
> Solaris 2.7/Sparc. I am using GLib 1.3.1.
> 
> #include <glib.h>
> 
> #define SIZE 4096
> 
> static GMemChunk *test = NULL;
> 
> int
> main ()
> {
>   guint8 *test1, *test2, *test3;
>   gint i;
> 
>   test = g_mem_chunk_new ("test mem chunk",
> 			  SIZE,
> 			  16, G_ALLOC_AND_FREE);

the third argument here should be SIZE*16 if you want 16 atoms preallocated,
since each atom is actually 4096 bytes in size (and not sizeof(guint8)==1).
you can also use the more convenient wraper macro g_mem_chunk_create(),
where you just pass in the real type that you are allocating, with
that you'd write:

test = g_mem_chunk_create (guint8[SIZE], 16, G_ALLOC_AND_FREE);

>   
>   test1 = g_chunk_new (guint8, test);
>   test2 = g_chunk_new (guint8, test);
> 
>   for (i=0; i<SIZE; i++)
>     test2[i] = 'b';
> 
>   test3 = g_chunk_new (guint8, test);
>   return 0;
> }
> 
> Here is the stack trace:
> 
> #0  0xff2c5518 in _malloc_unlocked ()
> #1  0xff2c5314 in malloc ()
> #2  0xff35a5cc in g_malloc (size=4096) at gmem.c:177
> #3  0xff35ac08 in g_mem_chunk_alloc (mem_chunk=0x21a20) at gmem.c:663
> #4  0x10b60 in main () at mcbirnan.c:23
> 
> -- 
> To unsubscribe: mail -s unsubscribe gtk-list-request@redhat.com < /dev/null
> 
> 

---
ciaoTJ



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