r20631 - gnucash/trunk/src/app-utils - Correct memory leaks found with Valgrind.

J. Alex Aycinena alex.aycinena at code.gnucash.org
Wed May 11 18:17:56 EDT 2011


Author: alex.aycinena
Date: 2011-05-11 18:17:56 -0400 (Wed, 11 May 2011)
New Revision: 20631
Trac: http://svn.gnucash.org/trac/changeset/20631

Modified:
   gnucash/trunk/src/app-utils/gnc-ui-util.c
Log:
Correct memory leaks found with Valgrind.

Modified: gnucash/trunk/src/app-utils/gnc-ui-util.c
===================================================================
--- gnucash/trunk/src/app-utils/gnc-ui-util.c	2011-05-11 21:52:39 UTC (rev 20630)
+++ gnucash/trunk/src/app-utils/gnc-ui-util.c	2011-05-11 22:17:56 UTC (rev 20631)
@@ -464,14 +464,6 @@
 
     gboolean tax_related = FALSE;
     const char *code;
-    const gchar *tax_type;
-    GNCAccountType atype;
-    SCM category;
-    SCM code_scm;
-    SCM tax_entity_type;
-    const gchar *form, *desc, *copy_txt;
-    gint64 copy_number;
-    SCM scm;
 
     if (!account)
         return NULL;
@@ -498,8 +490,13 @@
     }
     else  /* with tax code */
     {
-        const gchar *num_code = NULL;
+        const gchar *tax_type;
+        GNCAccountType atype;
+        SCM tax_entity_type;
+        SCM category;
+        gchar *num_code = NULL;
         const gchar *prefix = "N";
+        gchar *return_string = NULL;
 
         tax_type = gnc_get_current_book_tax_type ();
         if (tax_type == NULL || (safe_strcmp (tax_type, "") == 0))
@@ -562,89 +559,129 @@
                                           (atype == ACCT_TYPE_PAYABLE)) ?
                                          "txf-liab-eq-categories" : ""))));
 
-        num_code = g_strdup (code);
-        if (g_str_has_prefix (num_code, prefix))
-            num_code++; /* to lose the leading N */
-
-        if (category == SCM_UNDEFINED)
+        if (g_str_has_prefix (code, prefix))
         {
-            if (tax_related)
-                return g_strdup_printf
-                       (_("Tax type %s: invalid code %s for account type"),
-                        tax_type, num_code);
-            else
-                return g_strdup_printf
-                       (_("Not tax-related; tax type %s: invalid code %s for account type"),
-                        tax_type, num_code);
+            const gchar *num_code_tmp;
+            num_code_tmp = g_strdup (code);
+            num_code_tmp++; /* to lose the leading N */
+            num_code = g_strdup (num_code_tmp);
+            num_code_tmp--;
+            g_free ((gpointer *) num_code_tmp);
         }
-
-        code_scm = scm_str2symbol (code);
-        scm = scm_call_3 (get_form, category, code_scm, tax_entity_type);
-        if (!scm_is_string (scm))
+        else
         {
-            if (tax_related)
-                return g_strdup_printf
-                       (_("Invalid code %s for tax type %s"),
-                        num_code, tax_type);
-            else
-                return g_strdup_printf
-                       (_("Not tax-related; invalid code %s for tax type %s"),
-                        num_code, tax_type);
+            num_code = g_strdup (code);
         }
 
-        form = scm_to_locale_string (scm);
-        if (!form)
+        if (category == SCM_UNDEFINED)
         {
             if (tax_related)
-                return g_strdup_printf
-                       (_("No form: code %s, tax type %s"), num_code, tax_type);
+                return_string = g_strdup_printf
+                           (_("Tax type %s: invalid code %s for account type"),
+                            tax_type, num_code);
             else
-                return g_strdup_printf
-                       (_("Not tax-related; no form: code %s, tax type %s"),
-                        num_code, tax_type);
+                return_string = g_strdup_printf
+                           (_("Not tax-related; tax type %s: invalid code %s for account type"),
+                            tax_type, num_code);
         }
-
-        scm = scm_call_3 (get_desc, category, code_scm, tax_entity_type);
-        if (!scm_is_string (scm))
+        else
         {
-            if (tax_related)
-                return g_strdup_printf
-                       (_("No description: form %s, code %s, tax type %s"),
-                        form, num_code, tax_type);
+            SCM code_scm;
+            SCM form_scm;
+            code_scm = scm_str2symbol (code);
+            form_scm = scm_call_3 (get_form, category, code_scm, tax_entity_type);
+            if (!scm_is_string (form_scm))
+            {
+                if (tax_related)
+                    return_string =  g_strdup_printf
+                           (_("Invalid code %s for tax type %s"),
+                            num_code, tax_type);
+                else
+                    return_string =  g_strdup_printf
+                           (_("Not tax-related; invalid code %s for tax type %s"),
+                            num_code, tax_type);
+            }
             else
-                return g_strdup_printf
-                       (_("Not tax-related; no description: form %s, code %s, tax type %s"),
-                        form, num_code, tax_type);
+            {
+                gchar *form = NULL;
+                scm_dynwind_begin (0);
+                form = scm_to_locale_string (form_scm);
+                if (!form)
+                {
+                    if (tax_related)
+                        return_string = g_strdup_printf
+                               (_("No form: code %s, tax type %s"), num_code,
+                                tax_type);
+                    else
+                        return_string = g_strdup_printf
+                               (_("Not tax-related; no form: code %s, tax type %s"),
+                                num_code, tax_type);
+                }
+                else
+                {
+                    SCM desc_scm;
+                    desc_scm = scm_call_3 (get_desc, category, code_scm,
+                                            tax_entity_type);
+                    if (!scm_is_string (desc_scm))
+                    {
+                        if (tax_related)
+                            return_string = g_strdup_printf
+                               (_("No description: form %s, code %s, tax type %s"),
+                                form, num_code, tax_type);
+                        else
+                            return_string = g_strdup_printf
+                               (_("Not tax-related; no description: form %s, code %s, tax type %s"),
+                                form, num_code, tax_type);
+                    }
+                    else
+                    {
+                        gchar *desc = NULL;
+                        desc = scm_to_locale_string (desc_scm);
+                        if (!desc)
+                        {
+                            if (tax_related)
+                                return_string = g_strdup_printf
+                                  (_("No description: form %s, code %s, tax type %s"),
+                                  form, num_code, tax_type);
+                            else
+                                return_string = g_strdup_printf
+                                 (_("Not tax-related; no description: form %s, code %s, tax type %s"),
+                                    form, num_code, tax_type);
+                        }
+                        else
+                        {
+                            gint64 copy_number;
+                            gchar *copy_txt = NULL;
+                            copy_number = xaccAccountGetTaxUSCopyNumber (account);
+                            copy_txt = (copy_number == 1) ?
+                                        g_strdup ("") :
+                                        g_strdup_printf ("(%d)",
+                                                       (gint) copy_number);
+                            if (tax_related)
+                            {
+                                if (safe_strcmp (form, "") == 0)
+                                    return_string = g_strdup_printf ("%s", desc);
+                                else
+                                    return_string = g_strdup_printf ("%s%s: %s",
+                                                        form, copy_txt, desc);
+                            }
+                            else
+                            {
+                                return_string = g_strdup_printf
+                                    (_("Not tax-related; %s%s: %s (code %s, tax type %s)"),
+                                     form, copy_txt, desc, num_code, tax_type);
+                            }
+                            g_free (copy_txt);
+                        }
+                        scm_dynwind_free (desc);
+                    }
+                }
+                scm_dynwind_free (form);
+                scm_dynwind_end ();
+            }
         }
-
-        desc = scm_to_locale_string (scm);
-        if (!desc)
-        {
-            if (tax_related)
-                return g_strdup_printf
-                       (_("No description: form %s, code %s, tax type %s"),
-                        form, num_code, tax_type);
-            else
-                return g_strdup_printf
-                       (_("Not tax-related; no description: form %s, code %s, tax type %s"),
-                        form, num_code, tax_type);
-        }
-
-        copy_number = xaccAccountGetTaxUSCopyNumber (account);
-        copy_txt = (copy_number == 1) ? "" : g_strdup_printf ("(%d)",
-                   (gint) copy_number);
-
-        if (tax_related)
-        {
-            if (safe_strcmp (form, "") == 0)
-                return g_strdup_printf ("%s", desc);
-            else
-                return g_strdup_printf ("%s%s: %s", form, copy_txt, desc);
-        }
-        else
-            return g_strdup_printf
-                   (_("Not tax-related; %s%s: %s (code %s, tax type %s)"),
-                    form, copy_txt, desc, num_code, tax_type);
+        g_free (num_code);
+        return return_string;
     }
 }
 
@@ -678,12 +715,12 @@
          * account generally corresponds to a specific line number
          * on a paper form and each form has a unique
          * identification (e.g., Form 1040, Schedule A). */
-        return (sub_acct_tax_number == 0) ? g_strdup ("") :
+        return (sub_acct_tax_number == 0) ? NULL :
                g_strdup_printf (_("(Tax-related subaccounts: %d)"),
                                 sub_acct_tax_number);
     }
     else
-        return g_strdup ("");
+        return NULL;
 }
 
 static const char *



More information about the gnucash-changes mailing list