r17037 - gnucash/trunk/src - Fix a potential memory leak in gnc_dialog_get_string().
Andreas Köhler
andi5 at cvs.gnucash.org
Thu Mar 20 19:14:46 EDT 2008
Author: andi5
Date: 2008-03-20 19:14:45 -0400 (Thu, 20 Mar 2008)
New Revision: 17037
Trac: http://svn.gnucash.org/trac/changeset/17037
Modified:
gnucash/trunk/src/gnome-utils/gnc-dialog.c
gnucash/trunk/src/gnome-utils/gnc-dialog.h
gnucash/trunk/src/gnome-utils/test/test-gnc-dialog.c
gnucash/trunk/src/gnome/gnc-plugin-page-budget.c
Log:
Fix a potential memory leak in gnc_dialog_get_string().
Make the function return a non-const value and make it the
responsibility of the caller to free that. This is necessary because
not all possible types of input support a getter of an internal and
managed string.
Modified: gnucash/trunk/src/gnome/gnc-plugin-page-budget.c
===================================================================
--- gnucash/trunk/src/gnome/gnc-plugin-page-budget.c 2008-03-20 23:14:34 UTC (rev 17036)
+++ gnucash/trunk/src/gnome/gnc-plugin-page-budget.c 2008-03-20 23:14:45 UTC (rev 17037)
@@ -723,7 +723,7 @@
gpointer user_data)
{
GncPluginPageBudgetPrivate *priv = user_data;
- const gchar *name;
+ gchar *name;
gchar *desc;
gint num_periods;
GncRecurrence *gr;
@@ -737,10 +737,9 @@
if (name) {
gnc_budget_set_name(priv->budget, name);
DEBUG("%s", name);
+ g_free(name);
}
- //FIXME: this is special broken case where we actually do need to
- //free because widget is a GtkTextView
desc = (gchar *) gnc_dialog_get_string(d, "BudgetDescription");
gnc_budget_set_description(priv->budget, desc);
g_free(desc);
Modified: gnucash/trunk/src/gnome-utils/gnc-dialog.c
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-dialog.c 2008-03-20 23:14:34 UTC (rev 17036)
+++ gnucash/trunk/src/gnome-utils/gnc-dialog.c 2008-03-20 23:14:45 UTC (rev 17037)
@@ -354,23 +354,22 @@
return TRUE;
}
-const gchar * gnc_dialog_get_string(GncDialog *d, const gchar* name)
+gchar * gnc_dialog_get_string(GncDialog *d, const gchar* name)
{
SPECIFIC_INIT(d, name, wid, NULL);
if (IS_A(wid, "GtkEntry"))
- return gtk_entry_get_text(GTK_ENTRY(wid));
+ return g_strdup(gtk_entry_get_text(GTK_ENTRY(wid)));
else if (IS_A(wid, "GtkLabel"))
- return gtk_label_get_text(GTK_LABEL(wid));
+ return g_strdup(gtk_label_get_text(GTK_LABEL(wid)));
else if (IS_A(wid, "GtkCombo")) //deprecated
- return gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(wid)->entry));
+ return g_strdup(gtk_entry_get_text(GTK_ENTRY(GTK_COMBO(wid)->entry)));
else if (IS_A(wid, "GtkTextView")) {
GtkTextBuffer *buf;
GtkTextIter start, end;
buf = gtk_text_view_get_buffer(GTK_TEXT_VIEW(wid));
gtk_text_buffer_get_bounds(buf, &start, &end);
return gtk_text_buffer_get_text(buf, &start, &end, TRUE);
- //FIXME: LEAK: callers are expecting NOT to own the mem.
} else if (IS_A(wid, "GtkComboBoxEntry")) {
gint col;
GtkTreeModel *tm;
@@ -382,9 +381,9 @@
if (type != G_TYPE_STRING)
return NULL;
if (!gtk_combo_box_get_active_iter(GTK_COMBO_BOX(wid), &iter)) {
- GValue val;
- gtk_tree_model_get_value(tm, &iter, col, &val);
- return g_value_get_string(&val);
+ gchar *str;
+ gtk_tree_model_get(tm, &iter, col, &str);
+ return str;
} else return NULL;
} else TYPE_ERROR(wid, "GtkEntry or GtkLabel or GtkTextView", NULL);
}
Modified: gnucash/trunk/src/gnome-utils/gnc-dialog.h
===================================================================
--- gnucash/trunk/src/gnome-utils/gnc-dialog.h 2008-03-20 23:14:34 UTC (rev 17036)
+++ gnucash/trunk/src/gnome-utils/gnc-dialog.h 2008-03-20 23:14:45 UTC (rev 17037)
@@ -127,7 +127,7 @@
/* Type-specific getter/setters */
gboolean gnc_dialog_set_string(GncDialog *d, const char* name,
const gchar* val);
-const gchar* gnc_dialog_get_string(GncDialog *d, const char* name);
+gchar* gnc_dialog_get_string(GncDialog *d, const char* name);
gboolean gnc_dialog_set_double(GncDialog *d, const char* name, gdouble val);
gdouble gnc_dialog_get_double(GncDialog *d, const char* name);
Modified: gnucash/trunk/src/gnome-utils/test/test-gnc-dialog.c
===================================================================
--- gnucash/trunk/src/gnome-utils/test/test-gnc-dialog.c 2008-03-20 23:14:34 UTC (rev 17036)
+++ gnucash/trunk/src/gnome-utils/test/test-gnc-dialog.c 2008-03-20 23:14:45 UTC (rev 17037)
@@ -19,7 +19,7 @@
static gboolean apply_cb (GncDialog *pw, gpointer _n)
{
- const gchar *s;
+ gchar *s;
gdouble d;
gpointer p;
gboolean b;
@@ -29,6 +29,7 @@
printf("Entry: %s\n", s);
s = gnc_dialog_get_string(pw, "SampleLabel");
printf("Label: %s\n", s);
+ g_free(s);
p = gnc_dialog_get_custom(pw, "SampleSpinButton");
d = *(double *)p;
More information about the gnucash-changes
mailing list