Re: Memory leak
- From: Rajesh Padalia <rpadalia qatar net qa>
- To: Bijoy Chandrasekharan <bijoy_mc yahoo co in>
- Cc: gtk-app-devel-list <gtk-app-devel-list gnome org>
- Subject: Re: Memory leak
- Date: Sat, 30 Nov 2002 15:24:44 +0300
Many thanks for reply. I have converted all "g_strdup_printf" to local
variable and it is much better now, however it still leaves little
memory hole everytime I run this callback. This callback goes through
many long text files sevral times in one go. New code is attcahed after
this message.
In another reply Tristan said I need to use strfreev. Is it not enough
to allocate memory to a gpointer, use that gpointer for various
variables and free memory block?
Thanks and Regards,
Rajesh
------------
void
on_Get_Config_clicked (GtkButton *button,
gpointer user_data)
{
GtkWidget * gwGet_Config_clist;
GtkWidget * gwGet_Config_Block_Count_entry;
GtkWidget * gwGet_Config_Parameter_entry;
GtkWidget * gwParameter_combo =
lookup_widget(GTK_WIDGET(button),"Get_Config_combo_entry");
GtkWidget * gwSearch_clist =
lookup_widget(GTK_WIDGET(button),"Search_clist");
gpointer memoryblock1;
gpointer memoryblock2;
gpointer memoryblock3;
gpointer memoryblock4;
gpointer memoryblock5;
gpointer memoryblock6;
gpointer memoryblock7;
gpointer memoryblock8;
gchar * Parameter;
gchar ** Search_clist_col;
gchar ** Parameter_Value;
gchar ** Config_Results;
gchar * Get_Config_Tmp1;
gchar * Get_Config_Tmp2;
gchar * Get_Config_Tmp3;
gchar * Get_Config_Tmp4;
FILE *fconfig;
gchar Config[60];
gboolean CompdFound = FALSE;
gint Search_clist_no_of_rows = 0;
gint Search_clist_row_count = 0;
memoryblock1 = g_malloc(512);
memoryblock2 = g_malloc(512);
memoryblock3 = g_malloc(512);
memoryblock4 = g_malloc(512);
memoryblock5 = g_malloc(512);
memoryblock6 = g_malloc(512);
memoryblock7 = g_malloc(512);
memoryblock8 = g_malloc(512);
Get_Config_Tmp1 = (gchar *) memoryblock1;
Get_Config_Tmp2 = (gchar *) memoryblock2;
Get_Config_Tmp3 = (gchar *) memoryblock3;
Get_Config_Tmp4 = (gchar *) memoryblock4;
Parameter_Value = (gchar **) memoryblock5;
Search_clist_col = (gchar **) memoryblock6;
Config_Results = (gchar **) memoryblock7;
Parameter = (gchar *) memoryblock7;
Search_clist_col = g_strsplit(" = = = ", "=", 3);
Config_Results = g_strsplit(" = = = = ", "=", 4);
Parameter = gtk_entry_get_text(GTK_ENTRY(gwParameter_combo));
Get_Config_Window = create_Get_Config_Info();
gwGet_Config_clist =
lookup_widget(GTK_WIDGET(Get_Config_Window),"Get_Config_clist");
gwGet_Config_Block_Count_entry =
lookup_widget(GTK_WIDGET(Get_Config_Window),"Get_Config_Block_Count_entry");
gwGet_Config_Parameter_entry =
lookup_widget(GTK_WIDGET(Get_Config_Window),"Get_Config_Parameter_entry");
gtk_clist_clear(GTK_CLIST(gwGet_Config_clist));
Search_clist_no_of_rows = GTK_CLIST(gwSearch_clist)->rows;
Get_Config_Tmp1 = g_strdup_printf("%d", Search_clist_no_of_rows);
Get_Config_Tmp4 = g_strdup_printf(" %-7s= ", Parameter);
gtk_entry_set_text(GTK_ENTRY(gwGet_Config_Block_Count_entry),
Get_Config_Tmp1);
gtk_entry_set_text(GTK_ENTRY(gwGet_Config_Parameter_entry),
Parameter);
if (Search_clist_no_of_rows > 0) {
do {
gtk_clist_get_text(GTK_CLIST(gwSearch_clist),
Search_clist_row_count, 0, &Search_clist_col[0]);
gtk_clist_get_text(GTK_CLIST(gwSearch_clist),
Search_clist_row_count, 1, &Search_clist_col[1]);
gtk_clist_get_text(GTK_CLIST(gwSearch_clist),
Search_clist_row_count, 2, &Search_clist_col[2]);
gtk_clist_get_text(GTK_CLIST(gwSearch_clist),
Search_clist_row_count, 3, &Search_clist_col[3]);
Get_Config_Tmp2 = g_strdup_printf("NAME = %s:%s\n",
Search_clist_col[1], Search_clist_col[2]);
Get_Config_Tmp3 = g_strdup_printf("NAME = %s\n",
Search_clist_col[1]);
Parameter_Value = g_strsplit("X=Not Available", "=", 1);
fconfig = fopen(g_strjoin(NULL, CPNameDir, Search_clist_col[0],
NULL), "r");
while (!feof(fconfig)) {
fgets(Config, 60, fconfig);
if (!feof(fconfig)) {
if (g_strcasecmp(Search_clist_col[3], "COMPND") != 0) {
if (g_strcasecmp(Config, Get_Config_Tmp2) == 0) {
CompdFound = TRUE;
}
}
else {
if (g_strcasecmp(Config, Get_Config_Tmp3) == 0) {
CompdFound = TRUE;
Search_clist_col[2] = g_strdup(" ");
}
}
if ((strstr(Config, Get_Config_Tmp4))&&(CompdFound == TRUE)) {
Parameter_Value = g_strsplit(Config, "=", 1);
g_strstrip(Parameter_Value[1]);
CompdFound = FALSE;
break;
}
if ((g_strcasecmp(Config, "END\n") == 0)&&(CompdFound == TRUE))
{
Parameter_Value = g_strsplit("X=Not Available", "=", 1);
CompdFound = FALSE;
break;
}
}
}
fclose(fconfig);
Config_Results[0] = g_strdup(Search_clist_col[0]);
Config_Results[1] = g_strdup(Search_clist_col[1]);
Config_Results[2] = g_strdup(Search_clist_col[2]);
Config_Results[3] = g_strdup(Search_clist_col[3]);
Config_Results[4] = g_strdup(Parameter_Value[1]);
gtk_clist_append(GTK_CLIST(gwGet_Config_clist), Config_Results);
Search_clist_row_count++;
} while (Search_clist_row_count < Search_clist_no_of_rows);
g_free(memoryblock1);
g_free(memoryblock2);
g_free(memoryblock3);
g_free(memoryblock4);
g_free(memoryblock5);
g_free(memoryblock6);
g_free(memoryblock7);
g_free(memoryblock8);
gtk_widget_show(Get_Config_Window);
}
else {
Show_Error_Dialog("No Tags in Search List. Use Tag Search to have
minimum one entry in the list");
}
}
----------------
Bijoy Chandrasekharan wrote:
**********************from glib API
g_strdup_printf ()
gchar* g_strdup_printf (const gchar *format,
...);
Similar to the standard C sprintf() function but safer, since it
calculates the maximum space required and allocates memory to hold the
result. The returned string should be freed when no longer needed.
************************************************
Hi Rajesh,
you can 'g_strdup_printf' to a local variable and compre it with
'Config' and freee that variable after use.
hope this helps
bijoy.
while (!feof(fconfig)) {
fgets(Config, 60, fconfig);
if (!feof(fconfig)) {
if (g_strcasecmp(Search_clist_col[3], "COMPND") != 0) {
if (g_strcasecmp(Config, g_strdup_printf("NAME = %s:%s\n",
Search_clist_col[1], Search_clist_col[2]))
== 0) {
CompdFound = TRUE;
}
}
else {
if (g_strcasecmp(Config, g_strdup_printf("NAME = %s\n",
Search_clist_col[1])) == 0) {
CompdFound = TRUE;
Search_clist_col[2] = g_strdup(" ");
}
}
if ((strstr(Config, g_strdup_printf(" %-7s= ",
Parameter)))&&(CompdFound == TRUE)) {
Parameter_Value = g_strsplit(Config, "=", 1);
g_strstrip(Parameter_Value[1]);
CompdFound = FALSE;
break;
}
________________________________________________________________________
Missed your favourite TV serial last night? Try the new, Yahoo! TV.
visit http://in.tv.yahoo.com
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-app-devel-list
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]