gnucash maint: [gnc-budget.c] gnc_budget_get_account_period_note to be freed by the caller

Christopher Lam clam at code.gnucash.org
Tue Aug 24 11:02:56 EDT 2021


Updated	 via  https://github.com/Gnucash/gnucash/commit/c55ab503 (commit)
	from  https://github.com/Gnucash/gnucash/commit/9ddb9e82 (commit)



commit c55ab503495dd99e00f9d2421d37529e3e2daa55
Author: Christopher Lam <christopher.lck at gmail.com>
Date:   Mon Aug 23 23:34:31 2021 +0800

    [gnc-budget.c] gnc_budget_get_account_period_note to be freed by the caller

diff --git a/gnucash/gnome/gnc-budget-view.c b/gnucash/gnome/gnc-budget-view.c
index 851edc8b9..924db4c25 100644
--- a/gnucash/gnome/gnc-budget-view.c
+++ b/gnucash/gnome/gnc-budget-view.c
@@ -937,7 +937,7 @@ query_tooltip_tree_view_cb (GtkWidget *widget, gint x, gint y,
     GncBudgetViewPrivate *priv = GNC_BUDGET_VIEW_GET_PRIVATE(view);
     GtkTreePath          *path  = NULL;
     GtkTreeViewColumn    *column = NULL;
-    const gchar          *note;
+    gchar                *note;
     guint                 period_num;
     Account              *account;
 
@@ -962,6 +962,7 @@ query_tooltip_tree_view_cb (GtkWidget *widget, gint x, gint y,
     gtk_tooltip_set_text (tooltip, note);
     gtk_tree_view_set_tooltip_cell (tree_view, tooltip, path, column, NULL);
     gtk_tree_path_free (path);
+    g_free (note);
 
     return TRUE;
 }
diff --git a/gnucash/gnome/gnc-plugin-page-budget.c b/gnucash/gnome/gnc-plugin-page-budget.c
index 8a968bdf0..67da7eba2 100644
--- a/gnucash/gnome/gnc-plugin-page-budget.c
+++ b/gnucash/gnome/gnc-plugin-page-budget.c
@@ -1219,7 +1219,7 @@ gnc_plugin_page_budget_cmd_budget_note(GtkAction *action,
     GtkWidget *dialog, *note;
     gint result;
     GtkBuilder *builder;
-    const gchar *txt;
+    gchar *txt;
     GtkTreeViewColumn *col = NULL;
     GtkTreePath *path = NULL;
     guint period_num = 0;
@@ -1268,6 +1268,7 @@ gnc_plugin_page_budget_cmd_budget_note(GtkAction *action,
     note = GTK_WIDGET(gtk_builder_get_object(builder, "BudgetNote"));
     txt  = gnc_budget_get_account_period_note(priv->budget, acc, period_num);
     xxxgtk_textview_set_text(GTK_TEXT_VIEW(note), txt);
+    g_free (txt);
 
     gtk_widget_show_all(dialog);
     result = gtk_dialog_run(GTK_DIALOG(dialog));
diff --git a/libgnucash/engine/gnc-budget.c b/libgnucash/engine/gnc-budget.c
index dc67c389a..db4b3ec5d 100644
--- a/libgnucash/engine/gnc-budget.c
+++ b/libgnucash/engine/gnc-budget.c
@@ -631,20 +631,23 @@ gnc_budget_set_account_period_note(GncBudget *budget, const Account *account,
 
 }
 
-const gchar *
+gchar *
 gnc_budget_get_account_period_note(const GncBudget *budget,
                                    const Account *account, guint period_num)
 {
     gchar path_part_one [GUID_ENCODING_LENGTH + 1];
     gchar path_part_two [GNC_BUDGET_MAX_NUM_PERIODS_DIGITS];
     GValue v = G_VALUE_INIT;
+    gchar *retval;
 
     g_return_val_if_fail(GNC_IS_BUDGET(budget), NULL);
     g_return_val_if_fail(account, NULL);
 
     make_period_path (account, period_num, path_part_one, path_part_two);
     qof_instance_get_kvp (QOF_INSTANCE (budget), &v, 3, GNC_BUDGET_NOTES_PATH, path_part_one, path_part_two);
-    return (G_VALUE_HOLDS_STRING(&v)) ? g_value_get_string(&v) : NULL;
+    retval = G_VALUE_HOLDS_STRING (&v) ? g_value_dup_string(&v) : NULL;
+    g_value_unset (&v);
+    return retval;
 }
 
 time64
diff --git a/libgnucash/engine/gnc-budget.h b/libgnucash/engine/gnc-budget.h
index 3f87816ce..f5b9a2cee 100644
--- a/libgnucash/engine/gnc-budget.h
+++ b/libgnucash/engine/gnc-budget.h
@@ -156,9 +156,11 @@ gnc_numeric gnc_budget_get_account_period_value(
 gnc_numeric gnc_budget_get_account_period_actual_value(
     const GncBudget *budget, Account *account, guint period_num);
 
+/* get/set the budget account period's note, beware when retrieving
+   the period note, the latter must be g_freed by the caller */
 void gnc_budget_set_account_period_note(GncBudget *budget,
     const Account *account, guint period_num, const gchar *note);
-const gchar *gnc_budget_get_account_period_note(const GncBudget *budget,
+gchar *gnc_budget_get_account_period_note (const GncBudget *budget,
     const Account *account, guint period_num);
 
 /* Returns some budget in the book, or NULL. */



Summary of changes:
 gnucash/gnome/gnc-budget-view.c        | 3 ++-
 gnucash/gnome/gnc-plugin-page-budget.c | 3 ++-
 libgnucash/engine/gnc-budget.c         | 7 +++++--
 libgnucash/engine/gnc-budget.h         | 4 +++-
 4 files changed, 12 insertions(+), 5 deletions(-)



More information about the gnucash-changes mailing list