GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Wed Jul 30, 2014 12:57 am

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: g_key_file_get_value causes a memory leak?
PostPosted: Thu Oct 17, 2013 2:50 pm 
Offline
Familiar Face

Joined: Fri Oct 26, 2012 10:49 am
Posts: 35
Hello!
Valgrind memcheck compalins a memory leak at g_key_file_get_value, but I free the pointer at the end of the function. Can somebody says me what is wrong?

Quote:
==15891== 2 bytes in 1 blocks are definitely lost in loss record 15 of 5,009
==15891== at 0x4027172: malloc (vg_replace_malloc.c:270)
==15891== by 0x4889703: g_malloc (in /lib/libglib-2.0.so.0.2600.1)
==15891== by 0x48A2388: g_strdup (in /lib/libglib-2.0.so.0.2600.1)
==15891== by 0x487AB53: g_key_file_get_value (in /lib/libglib-2.0.so.0.2600.1)
==15891== by 0x807FBA2: re_load_descr_min_max (recipeedit.c:1639)

==15891== by 0x807C137: re_load_dflt_min_max_files (recipeedit.c:547)
==15891== by 0x80843A4: re_init (recipeedit.c:2849)
==15891== by 0x8091890: starts_init (startscreen.c:210)
==15891== by 0x8091A5B: startscreen_init (startscreen.c:279)
==15891== by 0x80733AD: main (main.c:225)

Code:
static int re_load_descr_min_max(void)
{
   gchar *p_strDValue;
   
   p_strDValue =  g_key_file_get_value(re_local.file_min, "step spin", "spintime", NULL);  // <--- line 1639
   if (p_strDValue !=NULL)
....
   p_strDValue =  g_key_file_get_value(re_local.file_min, "step spin", "spinspeed", NULL); 
   if (p_strDValue !=NULL)
....

etc.... in the next code I use always the same pointer p_strDValue to get my values from the file

At the end of the same function

   if (p_strDValue)
      g_free(p_strDValue);

   return 0;
}


Top
 Profile  
 
 Post subject: Re: g_key_file_get_value causes a memory leak?
PostPosted: Thu Oct 17, 2013 6:12 pm 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 734
Location: UK
Hello,

Not sure why you are calling g_key_file_get_value() twice in your function and not freeing the result in-between. Without knowing a bit more about your code I would say that this has a memory leak.

_________________
E.


Top
 Profile  
 
 Post subject: Re: g_key_file_get_value causes a memory leak?
PostPosted: Fri Oct 18, 2013 5:31 am 
Offline
Familiar Face

Joined: Fri Oct 26, 2012 10:49 am
Posts: 35
I read some parameters from a g_key_file into a local variable, check if it is a valid value and convert it into a numeric one if required, and then put the valid value into a set of global variables. In this case I read for each parameter the default value, the minimal valid value, the maximal valid value and the description to show.
for ex. speed should be between 0 and 10.000 rpm, description is "spin speed"
I put the result into a global structure:
global.spindflt, global.spin.min, global.spin.max, global.spin.descr...
and the I can use these variables during the editing of a recipe.

The nearly complete code is

Code:
static int re_load_descr_min_max(void)
{
   gchar *p_strDValue;
   char strDValue[1024];
//min
   descr_editBuffer.step.spin.speed.min =  g_key_file_get_integer(re_local.file_min, "step spin", "speed", NULL);
   descr_editBuffer.step.spin.acceleration.min =  g_key_file_get_integer(re_local.file_min, "step spin", "acceleration", NULL);
   p_strDValue =  g_key_file_get_value(re_local.file_min, "step spin", "spintime", NULL);  // min value
   if (p_strDValue !=NULL)
   {
      strcpy(strDValue, p_strDValue);
      ts_returnDoubleFromString(strDValue, &(descr_editBuffer.step.spin.spintime.min), strlen(strDValue) ) ;
   }
//max
   descr_editBuffer.step.spin.speed.max =  g_key_file_get_integer(re_local.file_max, "step spin", "speed", NULL);
   descr_editBuffer.step.spin.acceleration.max =  g_key_file_get_integer(re_local.file_max, "step spin", "acceleration", NULL);
   p_strDValue =  g_key_file_get_value(re_local.file_max, "step spin", "spintime", NULL);  // min value
   if (p_strDValue !=NULL)
   {
      strcpy(strDValue, p_strDValue);
      ts_returnDoubleFromString(strDValue, &(descr_editBuffer.step.spin.spintime.max), strlen(strDValue) ) ;
   }

// description
   p_strDValue =  =  g_key_file_get_string(re_local.file_descr, "step spin", "speed", NULL);
if (p_strDValue !=NULL)
      strcpy(sdescr_editBuffer.step.spin.acceleration.descr, p_strDValue);

   p_strDValue =  =  g_key_file_get_string(re_local.file_descr, "step spin", "acceleration", NULL);
if (p_strDValue !=NULL)
      strcpy(sdescr_editBuffer.step.spin.acceleration.descr, p_strDValue);
etc.....

//end
   if (p_strDValue)
      g_free(p_strDValue);

   return 0;
}



The integers don't make any problems, only the values which are read strings or the values I read first as a string and convert them.
If I freed the variable p_strDValue each time, should I declare it each time again?

Code:
gchar *p_strDValue;
p_strDValue =  =  g_key_file_get_string(.1..)
g_free p_strDValue;
p_strDValue =  =  g_key_file_get_string(..2.)
g_free p_strDValue;
etc...
??


Thanks a lot for your help

MBoerner


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

All times are UTC


Who is online

Users browsing this forum: No registered users 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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group