GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Nov 29, 2014 6:13 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: How to free the GSList Memory Allocation
PostPosted: Mon Jul 30, 2007 2:40 am 
Offline

Joined: Mon Jul 30, 2007 2:15 am
Posts: 2
Hello everybody, I have a problem in GSList memory allocation.

Here is my source code:
Code:
#include <stdio.h>
#include <glib.h>

void display_list(GSList *list)
{
    GSList *iterator = NULL;

     printf("print the data:\n");
    //print the list data
    for (iterator = list; iterator; iterator = iterator->next) {
        printf("%s ", (char*)iterator->data);
    }
    printf("\n");
}

int main(int argc, char *argv[])
{
    GSList *list = NULL;

    //set the GMemVTable to the default table.
    g_mem_set_vtable(glib_mem_profiler_table);

    //call g_mem_profile() when the application exits
    g_atexit(g_mem_profile);

    printf("Creat single list:\n");

    //add data to list
    list = g_slist_append(list, "first");
    list = g_slist_append(list, "second");
    list = g_slist_append(list, "third");

    //print the list data
    display_list(list);

    //Free the list
    g_slist_free(list);
    return 0;
}


And here is execute result:
Quote:
Creat single list:
first second third
GLib Memory statistics (successful operations):
blocks of | allocated | freed | allocated | freed | n_bytes
n_bytes | n_times by | n_times by | n_times by | n_times by | remaining | malloc() | free() | realloc() | realloc() |
===========|============|============|============|============|===========
508 | 3 | 0 | 0 | 0 | +1524
2040 | 1 | 0 | 0 | 0 | +2040
GLib Memory statistics (failing operations):
--- none ---
Total bytes: allocated=3564, zero-initialized=3564 (100.00%), freed=0 (0.00%), remaining=3564


How to free to 3564 bytes (508*3+2040) memory space?
Please you help me.
Thank you very much!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 6:44 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
I'm not in the mindset at the moment to *really* analyze this code, however, I don't see anything wrong with it. The thing is, memory management as handled by the OS is a tricky subject--which is why memory management software or library calls can be misleading.

Again, I'm in no state of mind to draw any conclusions... but your "memory leak" may be suspect.

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 8:49 am 
Offline

Joined: Mon Jul 30, 2007 2:15 am
Posts: 2
I found the memory leak may be caused by memory slice.
I test the new code as follow:

Code:
#include <glib.h>
#define SLICE_SIZE 10

int main(int argc, int *argv[])
{
   gchar *strings[100];
   gint i;

   g_mem_set_vtable(glib_mem_profiler_table);
   g_atexit(g_mem_profile);

   for(i=0; i< 100; i++)
      strings[i] = g_slice_alloc(SLICE_SIZE);
   for(i=0; i<100; i++)
      g_slice_free1(SLICE_SIZE, strings[i]);
   return 0;
}


Then the "GLib Memory statistics" also allocated 3564 bytes memory space and cannot free.
Can any one help me solve this problem?
thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 12:04 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 3:07 am
Posts: 384
Location: Fairfax, Virginia
Memory allocation is a tricky thing, and there are a lot of things going on behind the scenes. You are allocating 100 strings of length 10, but that is not the actual memory footprint because there is overhead in how you allocated. A better way to use this example would be the following:

Code:
#include <glib.h>

int main(int argc, int *argv[])
{
   gchar *strings[100];
   gint i;

   g_mem_set_vtable (glib_mem_profiler_table);
   g_atexit (g_mem_profile);

   for (i = 0; i < 100; i++)
   {
      strings[i] = g_strdup ("0123456789");
   }
   
   for (i = 0; i < 100; i++)
      g_free (strings[i]);
   
   return 0;
}


In your example, nothing is freed because the memory is already just filled with zeros. In the example above, exactly what is created is destroyed.

GLib's vtable shouldn't be used as something to profile every aspect of your application because it only works in certain circumstances and can be quite tricky. I would recommend that you check out valgrind to perform memory profiling, as it is a much more accurate tool.

_________________
Andrew Krause

Foundations of GTK+ Development: Buy now for only $31.49!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group