Skip to content

Commit 97d189c

Browse files
qarkaicaclark
authored andcommitted
Use g_autoptr(GString), fix several memory leaks
Simplify related functions.
1 parent 0b20b06 commit 97d189c

21 files changed

+154
-284
lines changed

src/bar-gps.cc

+18-36
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ void bar_pane_gps_dnd_receive(GtkWidget *pane, GdkDragContext *,
150150
gint geocoded_count;
151151
gdouble latitude;
152152
gdouble longitude;
153-
GString *message;
154153

155154
pgd = static_cast<PaneGPSData *>(g_object_get_data(G_OBJECT(pane), "pane_data"));
156155
if (!pgd) return;
@@ -189,7 +188,7 @@ void bar_pane_gps_dnd_receive(GtkWidget *pane, GdkDragContext *,
189188

190189
if(count)
191190
{
192-
message = g_string_new("");
191+
g_autoptr(GString) message = g_string_new("");
193192
if (count == 1)
194193
{
195194
fd_found = static_cast<FileData *>(g_list_first(pgd->geocode_list)->data);
@@ -230,7 +229,6 @@ void bar_pane_gps_dnd_receive(GtkWidget *pane, GdkDragContext *,
230229
bar_pane_gps_close_save_cb, TRUE);
231230

232231
gtk_widget_show(gd->dialog);
233-
g_string_free(message, TRUE);
234232
}
235233
}
236234
}
@@ -307,7 +305,6 @@ gboolean bar_pane_gps_marker_keypress_cb(GtkWidget *widget, ClutterButtonEvent *
307305
ClutterActor *actor;
308306
ClutterActor *direction;
309307
ClutterActor *current_image;
310-
GString *text;
311308
gint height;
312309
gint width;
313310
GdkPixbufRotation rotate;
@@ -380,7 +377,7 @@ gboolean bar_pane_gps_marker_keypress_cb(GtkWidget *widget, ClutterButtonEvent *
380377
thumb_loader_start(tl, fd);
381378
}
382379

383-
text = g_string_new(fd->name);
380+
g_autoptr(GString) text = g_string_new(fd->name);
384381
g_string_append(text, "\n");
385382
g_string_append(text, text_from_time(fd->date));
386383
g_string_append(text, "\n");
@@ -396,8 +393,6 @@ gboolean bar_pane_gps_marker_keypress_cb(GtkWidget *widget, ClutterButtonEvent *
396393
champlain_marker_set_selection_color(&thumb_colour);
397394
champlain_marker_set_selection_text_color(&text_colour);
398395

399-
g_string_free(text, TRUE);
400-
401396
parent_marker = clutter_actor_get_parent(label_marker);
402397
if (clutter_actor_get_n_children(parent_marker ) > 1 )
403398
{
@@ -439,18 +434,14 @@ gboolean bar_pane_gps_create_markers_cb(gpointer data)
439434
ClutterActor *direction;
440435
ClutterColor marker_colour = { MARKER_COLOUR };
441436
ClutterColor thumb_colour = { THUMB_COLOUR };
442-
GString *message;
443437
ClutterContent *canvas;
444438

439+
const gint selection_added = pgd->selection_count - g_list_length(pgd->not_added);
445440
gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(pgd->progress),
446-
static_cast<gdouble>(pgd->selection_count - g_list_length(pgd->not_added)) /
447-
static_cast<gdouble>(pgd->selection_count));
441+
static_cast<gdouble>(selection_added) / static_cast<gdouble>(pgd->selection_count));
448442

449-
message = g_string_new("");
450-
g_string_printf(message, "%u/%i",
451-
pgd->selection_count - g_list_length(pgd->not_added), pgd->selection_count);
452-
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgd->progress), message->str);
453-
g_string_free(message, TRUE);
443+
g_autofree gchar *message = g_strdup_printf("%u/%i", selection_added, pgd->selection_count);
444+
gtk_progress_bar_set_text(GTK_PROGRESS_BAR(pgd->progress), message);
454445

455446
if(pgd->not_added)
456447
{
@@ -720,26 +711,21 @@ void bar_pane_gps_slider_changed_cb(GtkScaleButton *slider,
720711
gpointer data)
721712
{
722713
auto pgd = static_cast<PaneGPSData *>(data);
723-
GString *message;
724714

725-
message = g_string_new("");
726-
g_string_printf(message, _("Zoom %i"), static_cast<gint>(zoom));
715+
g_autofree gchar *message = g_strdup_printf(_("Zoom %i"), static_cast<gint>(zoom));
727716

728717
g_object_set(G_OBJECT(CHAMPLAIN_VIEW(pgd->gps_view)), "zoom-level", static_cast<gint>(zoom), NULL);
729-
gtk_widget_set_tooltip_text(GTK_WIDGET(slider), message->str);
730-
g_string_free(message, TRUE);
731-
718+
gtk_widget_set_tooltip_text(GTK_WIDGET(slider), message);
732719
}
720+
733721
void bar_pane_gps_view_state_changed_cb(ChamplainView *view, GParamSpec *, gpointer data)
734722
{
735723
auto pgd = static_cast<PaneGPSData *>(data);
736724
ChamplainState status;
737725
gint zoom;
738-
GString *message;
739726

740727
g_object_get(G_OBJECT(view), "zoom-level", &zoom, NULL);
741-
message = g_string_new("");
742-
g_string_printf(message, _("Zoom level %i"), zoom);
728+
g_autofree gchar *message = g_strdup_printf(_("Zoom level %i"), zoom);
743729

744730
g_object_get(G_OBJECT(view), "state", &status, NULL);
745731
if (status == CHAMPLAIN_STATE_LOADING)
@@ -748,13 +734,11 @@ void bar_pane_gps_view_state_changed_cb(ChamplainView *view, GParamSpec *, gpoin
748734
}
749735
else
750736
{
751-
gtk_label_set_text(GTK_LABEL(pgd->state), message->str);
737+
gtk_label_set_text(GTK_LABEL(pgd->state), message);
752738
}
753739

754-
gtk_widget_set_tooltip_text(GTK_WIDGET(pgd->slider), message->str);
740+
gtk_widget_set_tooltip_text(GTK_WIDGET(pgd->slider), message);
755741
gtk_scale_button_set_value(GTK_SCALE_BUTTON(pgd->slider), static_cast<gdouble>(zoom));
756-
757-
g_string_free(message, TRUE);
758742
}
759743

760744
void bar_pane_gps_notify_cb(FileData *fd, NotifyType type, gpointer data)
@@ -815,27 +799,25 @@ GtkWidget *bar_pane_gps_menu(PaneGPSData *pgd)
815799
void bar_pane_gps_map_centreing(PaneGPSData *pgd)
816800
{
817801
GenericDialog *gd;
818-
GString *message = g_string_new("");
819802

803+
const gchar *message;
820804
if (pgd->centre_map_checked)
821805
{
822-
message = g_string_append(message, _("Move map centre to marker\n is disabled"));
823-
pgd->centre_map_checked = FALSE;
806+
message = _("Move map centre to marker\n is disabled");
824807
}
825808
else
826809
{
827-
message = g_string_append(message, _("Move map centre to marker\n is enabled"));
828-
pgd->centre_map_checked = TRUE;
810+
message = _("Move map centre to marker\n is enabled");
829811
}
830812

813+
pgd->centre_map_checked = !pgd->centre_map_checked;
814+
831815
gd = generic_dialog_new(_("Map centering"),
832816
"map_centering", nullptr, TRUE, nullptr, pgd);
833-
generic_dialog_add_message(gd, GQ_ICON_DIALOG_INFO, _("Map Centering"), message->str, TRUE);
817+
generic_dialog_add_message(gd, GQ_ICON_DIALOG_INFO, _("Map Centering"), message, TRUE);
834818
generic_dialog_add_button(gd, GQ_ICON_OK, "OK", nullptr, TRUE);
835819

836820
gtk_widget_show(gd->dialog);
837-
838-
g_string_free(message, TRUE);
839821
}
840822

841823
#if HAVE_GTK4

src/collect-dlg.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static void collection_save_or_append_dialog(gint type, CollectionData *cd)
152152

153153
collect_manager_list(&collection_list, nullptr, nullptr);
154154

155-
GString *out_string = g_string_new(nullptr);
155+
g_autoptr(GString) out_string = g_string_new(nullptr);
156156

157157
for (GList *work = collection_list; work != nullptr; work = work->next)
158158
{
@@ -163,7 +163,6 @@ static void collection_save_or_append_dialog(gint type, CollectionData *cd)
163163
g_list_free_full(collection_list, g_free);
164164

165165
existing_collections = gtk_label_new(out_string->str);
166-
g_string_free(out_string, TRUE);
167166

168167
scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
169168
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);

src/collect-io.cc

+1-3
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
148148
return FALSE;
149149
}
150150

151-
GString *extended_filename_buffer = g_string_new(nullptr);
151+
g_autoptr(GString) extended_filename_buffer = g_string_new(nullptr);
152152
while (fgets(s_buf, sizeof(s_buf), f))
153153
{
154154
gchar *buf;
@@ -341,8 +341,6 @@ static gboolean collection_load_private(CollectionData *cd, const gchar *path, C
341341
g_free(buffer2);
342342
}
343343

344-
g_string_free(extended_filename_buffer, TRUE);
345-
346344
DEBUG_1("collection files: total = %u fail = %u official=%d gqview=%d geometry=%d", total, fail, has_official_header, has_gqview_header, has_geometry_header);
347345

348346
fclose(f);

src/collect.cc

+10-15
Original file line numberDiff line numberDiff line change
@@ -373,30 +373,25 @@ gboolean is_collection(const gchar *param)
373373
*
374374
*
375375
*/
376-
void collection_contents(const gchar *name, GString **contents)
376+
GString *collection_contents(const gchar *name, GString *contents)
377377
{
378-
CollectionData *cd;
379-
CollectInfo *ci;
380-
GList *work;
381-
FileData *fd;
382-
383-
if (!is_collection(name)) return;
378+
if (!is_collection(name)) return contents;
384379

380+
CollectionData *cd = collection_new("");
385381
g_autofree gchar *path = collection_path(name);
386-
cd = collection_new("");
387382
collection_load(cd, path, COLLECTION_LOAD_APPEND);
388-
work = cd->list;
389-
while (work)
383+
384+
for (GList *work = cd->list; work; work = work->next)
390385
{
391-
ci = static_cast<CollectInfo *>(work->data);
392-
fd = ci->fd;
393-
*contents = g_string_append(*contents, fd->path);
394-
*contents = g_string_append(*contents, "\n");
386+
auto *ci = static_cast<CollectInfo *>(work->data);
395387

396-
work = work->next;
388+
contents = g_string_append(contents, ci->fd->path);
389+
contents = g_string_append(contents, "\n");
397390
}
398391

399392
collection_free(cd);
393+
394+
return contents;
400395
}
401396

402397
/**

src/collect.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ gboolean collection_window_modified_exists();
135135

136136
gboolean is_collection(const gchar *param);
137137
gchar *collection_path(const gchar *param);
138-
void collection_contents(const gchar *name, GString **contents);
138+
GString *collection_contents(const gchar *name, GString *contents) G_GNUC_WARN_UNUSED_RESULT;
139139
GList *collection_contents_fd(const gchar *name);
140140

141141
#endif

0 commit comments

Comments
 (0)