Skip to content

Commit a3eefc8

Browse files
[import-main-matcher.cpp] plug GtkWidget* and static char* leaks
static char* strbuf would never be completely freed upon exit. store a std::string->std::string map within the gui object, return the c_str(). also plug GtkWidget* leak.
1 parent fcf2fc5 commit a3eefc8

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

gnucash/import-export/import-main-matcher.cpp

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@
4141
#include <memory>
4242
#include <algorithm>
4343
#include <vector>
44+
#include <unordered_map>
45+
#include <string>
4446

4547
#include "import-main-matcher.h"
4648

@@ -64,6 +66,8 @@
6466
#define GNC_PREFS_GROUP "dialogs.import.generic.transaction-list"
6567
#define IMPORT_MAIN_MATCHER_CM_CLASS "transaction-matcher-dialog"
6668

69+
using StrStrMap = std::unordered_map<std::string,std::string>;
70+
6771
struct _main_matcher_info
6872
{
6973
GtkWidget *main_widget;
@@ -96,6 +100,7 @@ struct _main_matcher_info
96100
GHashTable *memo_hash;
97101

98102
GList *new_strings;
103+
StrStrMap colormap;
99104
};
100105

101106
enum downloaded_cols
@@ -242,6 +247,7 @@ gnc_gen_trans_list_delete (GNCImportMainMatcher *info)
242247
g_hash_table_destroy (info->desc_hash);
243248
g_hash_table_destroy (info->notes_hash);
244249
g_hash_table_destroy (info->memo_hash);
250+
info->colormap.~StrStrMap();
245251

246252
g_list_free_full (info->new_strings, (GDestroyNotify)g_free);
247253

@@ -1737,6 +1743,7 @@ gnc_gen_trans_common_setup (GNCImportMainMatcher *info,
17371743
info->memo_hash = g_hash_table_new (g_str_hash, g_str_equal);
17381744
info->new_strings = NULL;
17391745
info->transaction_processed_cb = NULL;
1746+
new (&info->colormap) StrStrMap();
17401747

17411748
/* Connect the signals */
17421749
gtk_builder_connect_signals_full (builder, gnc_builder_connect_full_func, info);
@@ -1861,18 +1868,22 @@ gnc_gen_trans_list_run (GNCImportMainMatcher *info)
18611868
}
18621869

18631870
static const gchar*
1864-
get_required_color (const gchar *class_name)
1871+
get_required_color (StrStrMap& cache, const gchar *class_name)
18651872
{
1866-
GdkRGBA color;
1867-
GtkWidget *label = gtk_label_new ("Color");
1868-
GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET(label));
1869-
gtk_style_context_add_class (context, class_name);
1870-
gnc_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &color);
1871-
static gchar *strbuf = NULL;
1872-
if (strbuf)
1873-
g_free (strbuf);
1874-
strbuf = gdk_rgba_to_string (&color);
1875-
return strbuf;
1873+
auto& rv = cache[class_name];
1874+
if (rv.empty())
1875+
{
1876+
GdkRGBA color;
1877+
GtkWidget *label = gtk_label_new ("Color");
1878+
GtkStyleContext *context = gtk_widget_get_style_context (GTK_WIDGET(label));
1879+
gtk_style_context_add_class (context, class_name);
1880+
gnc_style_context_get_background_color (context, GTK_STATE_FLAG_NORMAL, &color);
1881+
gchar* col_str = gdk_rgba_to_string (&color);
1882+
rv = col_str;
1883+
g_free (col_str);
1884+
gtk_widget_destroy (label);
1885+
}
1886+
return rv.c_str();
18761887
}
18771888

18781889
static void
@@ -2010,7 +2021,7 @@ refresh_model_row (GNCImportMainMatcher *gui,
20102021
if (gnc_import_TransInfo_is_balanced (info))
20112022
{
20122023
ro_text = _("New, already balanced");
2013-
color = get_required_color (int_not_required_class);
2024+
color = get_required_color (gui->colormap, int_not_required_class);
20142025
}
20152026
else
20162027
{
@@ -2025,7 +2036,7 @@ refresh_model_row (GNCImportMainMatcher *gui,
20252036
GNCPrintAmountInfo pinfo = gnc_commodity_print_info (
20262037
xaccAccountGetCommodity (dest_acc), true);
20272038
imbalance = g_strdup (xaccPrintAmount (bal_amt, pinfo));
2028-
color = get_required_color (int_not_required_class);
2039+
color = get_required_color (gui->colormap, int_not_required_class);
20292040
if (gnc_import_TransInfo_get_destacc_selected_manually (info))
20302041
{
20312042
text =
@@ -2049,7 +2060,7 @@ refresh_model_row (GNCImportMainMatcher *gui,
20492060
xaccTransGetCurrency (gnc_import_TransInfo_get_trans (info)), true);
20502061
gnc_numeric bal_val = gnc_import_TransInfo_get_dest_value (info);
20512062
imbalance = g_strdup (xaccPrintAmount (bal_val, pinfo));
2052-
color = get_required_color (int_required_class);
2063+
color = get_required_color (gui->colormap, int_required_class);
20532064
text =
20542065
/* Translators: %s is the amount to be transferred. */
20552066
g_strdup_printf (_("New, UNBALANCED (need price to transfer %s to acct %s)!"),
@@ -2065,7 +2076,7 @@ refresh_model_row (GNCImportMainMatcher *gui,
20652076
xaccTransGetCurrency (gnc_import_TransInfo_get_trans (info)), true);
20662077
gnc_numeric bal_val = gnc_import_TransInfo_get_dest_value (info);
20672078
imbalance = g_strdup (xaccPrintAmount (bal_val, pinfo));
2068-
color = get_required_color (int_prob_required_class);
2079+
color = get_required_color (gui->colormap, int_prob_required_class);
20692080
text =
20702081
/* Translators: %s is the amount to be transferred. */
20712082
g_strdup_printf (_("New, UNBALANCED (need acct to transfer %s)!"),
@@ -2083,7 +2094,7 @@ refresh_model_row (GNCImportMainMatcher *gui,
20832094
if (sel_match)
20842095
{
20852096
gchar *full_names = get_peer_acct_names (sel_match->split);
2086-
color = get_required_color (int_not_required_class);
2097+
color = get_required_color (gui->colormap, int_not_required_class);
20872098
if (gnc_import_TransInfo_get_match_selected_manually (info))
20882099
{
20892100
text = g_strdup_printf (_("Reconcile (manual) match to %s"),
@@ -2099,7 +2110,7 @@ refresh_model_row (GNCImportMainMatcher *gui,
20992110
}
21002111
else
21012112
{
2102-
color = get_required_color (int_required_class);
2113+
color = get_required_color (gui->colormap, int_required_class);
21032114
ro_text = _("Match missing!");
21042115
show_pixbuf = false;
21052116
remove_child_row (model, iter);
@@ -2112,7 +2123,7 @@ refresh_model_row (GNCImportMainMatcher *gui,
21122123
if (sel_match)
21132124
{
21142125
gchar *full_names = get_peer_acct_names (sel_match->split);
2115-
color = get_required_color (int_not_required_class);
2126+
color = get_required_color (gui->colormap, int_not_required_class);
21162127
if (gnc_import_TransInfo_get_match_selected_manually (info))
21172128
{
21182129
text = g_strdup_printf (_("Update and reconcile (manual) match to %s"),
@@ -2128,15 +2139,15 @@ refresh_model_row (GNCImportMainMatcher *gui,
21282139
}
21292140
else
21302141
{
2131-
color = get_required_color (int_required_class);
2142+
color = get_required_color (gui->colormap, int_required_class);
21322143
ro_text = _("Match missing!");
21332144
show_pixbuf = false;
21342145
remove_child_row (model, iter);
21352146
}
21362147
}
21372148
break;
21382149
case GNCImport_SKIP:
2139-
color = get_required_color (int_required_class);
2150+
color = get_required_color (gui->colormap, int_required_class);
21402151
ro_text = _("Do not import (no action selected)");
21412152
show_pixbuf = false;
21422153
remove_child_row (model, iter);

0 commit comments

Comments
 (0)