Skip to content

Commit

Permalink
Use g_autoptr(GError), fix several memory leaks
Browse files Browse the repository at this point in the history
Deduplicate and flatten related functions.
  • Loading branch information
qarkai authored and caclark committed Jan 10, 2025
1 parent bc5e15e commit 41ad5f5
Show file tree
Hide file tree
Showing 18 changed files with 84 additions and 133 deletions.
3 changes: 1 addition & 2 deletions src/desktop-file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ gboolean editor_window_save(EditorWindow *ew)
{
GtkTextIter start;
GtkTextIter end;
GError *error = nullptr;
gboolean ret = TRUE;
const gchar *name = gq_gtk_entry_get_text(GTK_ENTRY(ew->entry));

Expand All @@ -104,10 +103,10 @@ gboolean editor_window_save(EditorWindow *ew)
ret = FALSE;
}

g_autoptr(GError) error = nullptr;
if (ret && !g_file_set_contents(path, text, -1, &error))
{
file_util_warning_dialog(_("Can't save"), error->message, GQ_ICON_DIALOG_ERROR, nullptr);
g_error_free(error);
ret = FALSE;
}

Expand Down
3 changes: 1 addition & 2 deletions src/dupe.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5093,7 +5093,6 @@ static void export_duplicates_data_cancel_cb(FileDialog *, gpointer data)
static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)
{
auto edd = static_cast<ExportDupesData *>(data);
GError *error = nullptr;
GtkTreeModel *store;
GtkTreeIter iter;
DupeItem *di;
Expand All @@ -5111,11 +5110,11 @@ static void export_duplicates_data_save_cb(FileDialog *fdlg, gpointer data)

out_file = g_file_new_for_path(fdlg->dest_path);

g_autoptr(GError) error = nullptr;
gfstream = g_file_replace(out_file, nullptr, TRUE, G_FILE_CREATE_NONE, nullptr, &error);
if (error)
{
log_printf(_("Error creating Export duplicates data file: Error: %s\n"), error->message);
g_error_free(error);
return;
}

Expand Down
9 changes: 3 additions & 6 deletions src/exif-common.cc
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ static gchar *exif_build_formatted_DateTime(ExifData *exif)
gchar buf[128];
gint buflen;
struct tm tm;
GError *error = nullptr;

gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeOriginal");
if (text)
Expand All @@ -229,11 +228,11 @@ static gchar *exif_build_formatted_DateTime(ExifData *exif)
buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
if (buflen > 0)
{
g_autoptr(GError) error = nullptr;
g_autofree gchar *tmp = g_locale_to_utf8(buf, buflen, nullptr, nullptr, &error);
if (error)
{
log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
g_error_free(error);
}
else
{
Expand All @@ -257,7 +256,6 @@ static gchar *exif_build_formatted_DateTimeDigitized(ExifData *exif)
gchar buf[128];
gint buflen;
struct tm tm;
GError *error = nullptr;

gchar *text = exif_get_data_as_text(exif, "Exif.Photo.DateTimeDigitized");
if (text)
Expand All @@ -277,11 +275,11 @@ static gchar *exif_build_formatted_DateTimeDigitized(ExifData *exif)
buflen = strftime(buf, sizeof(buf), "%x %X", &tm);
if (buflen > 0)
{
g_autoptr(GError) error = nullptr;
g_autofree gchar *tmp = g_locale_to_utf8(buf, buflen, nullptr, nullptr, &error);
if (error)
{
log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
g_error_free(error);
}
else
{
Expand Down Expand Up @@ -759,7 +757,6 @@ static gchar *exif_build_formatted_localtime(ExifData *exif)
{
gchar buf[128];
gint buflen;
GError *error = nullptr;
struct tm *tm_local;
struct tm tm_utc;
time_t stamp;
Expand All @@ -786,11 +783,11 @@ static gchar *exif_build_formatted_localtime(ExifData *exif)
buflen = strftime(buf, sizeof(buf), "%x %X", tm_local);
if (buflen > 0)
{
g_autoptr(GError) error = nullptr;
g_autofree gchar *tmp = g_locale_to_utf8(buf, buflen, nullptr, nullptr, &error);
if (error)
{
log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
g_error_free(error);
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions src/filedata/filedata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ const gchar *FileData::text_from_time(time_t t)
gchar buf[128];
gint buflen;
struct tm *btime;
GError *error = nullptr;

btime = localtime(&t);

Expand All @@ -140,11 +139,11 @@ const gchar *FileData::text_from_time(time_t t)
if (buflen < 1) return "";

g_free(ret);
g_autoptr(GError) error = nullptr;
ret = g_locale_to_utf8(buf, buflen, nullptr, nullptr, &error);
if (error)
{
log_printf("Error converting locale strftime to UTF-8: %s\n", error->message);
g_error_free(error);
return "";
}

Expand Down
3 changes: 1 addition & 2 deletions src/image-load-pdf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct ImageLoaderPDF : public ImageLoaderBackend

gboolean ImageLoaderPDF::write(const guchar *buf, gsize &chunk_size, gsize count, GError **)
{
GError *poppler_error = nullptr;
g_autoptr(GError) poppler_error = nullptr;
PopplerPage *page;
PopplerDocument *document;
gdouble width;
Expand All @@ -76,7 +76,6 @@ gboolean ImageLoaderPDF::write(const guchar *buf, gsize &chunk_size, gsize count
if (poppler_error)
{
log_printf("warning: pdf reader error: %s\n", poppler_error->message);
g_error_free(poppler_error);
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions src/image-load-svgz.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ gchar **ImageLoaderSvgz::get_format_mime_types()

void ImageLoaderSvgz::init(AreaUpdatedCb area_updated_cb, SizePreparedCb size_prepared_cb, AreaPreparedCb area_prepared_cb, gpointer data)
{
GError *error = nullptr;
g_autoptr(GError) error = nullptr;

loader = gdk_pixbuf_loader_new_with_mime_type("image/svg", &error);
if (error)
{
g_error_free(error);
return;
}

Expand Down
59 changes: 29 additions & 30 deletions src/layout-image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -400,52 +400,50 @@ static void layout_image_animate_update_image(LayoutWindow *lw)

static void animation_async_ready_cb(GObject *, GAsyncResult *res, gpointer data)
{
GError *error = nullptr;
auto animation = static_cast<AnimationData *>(data);

if (animation)
if (!animation) return;

if (g_cancellable_is_cancelled(animation->cancellable))
{
if (g_cancellable_is_cancelled(animation->cancellable))
{
gdk_pixbuf_animation_new_from_stream_finish(res, nullptr);
g_object_unref(animation->in_file);
g_object_unref(animation->gfstream);
image_animation_data_free(animation);
return;
}
gdk_pixbuf_animation_new_from_stream_finish(res, nullptr);
g_object_unref(animation->in_file);
g_object_unref(animation->gfstream);
image_animation_data_free(animation);
return;
}

animation->gpa = gdk_pixbuf_animation_new_from_stream_finish(res, &error);
if (animation->gpa)
g_autoptr(GError) error = nullptr;
animation->gpa = gdk_pixbuf_animation_new_from_stream_finish(res, &error);
if (animation->gpa)
{
if (!gdk_pixbuf_animation_is_static_image(animation->gpa))
{
if (!gdk_pixbuf_animation_is_static_image(animation->gpa))
animation->iter = gdk_pixbuf_animation_get_iter(animation->gpa, nullptr);
if (animation->iter)
{
animation->iter = gdk_pixbuf_animation_get_iter(animation->gpa, nullptr);
if (animation->iter)
{
animation->data_adr = animation->lw->image->image_fd;
animation->delay = gdk_pixbuf_animation_iter_get_delay_time(animation->iter);
animation->valid = TRUE;
animation->data_adr = animation->lw->image->image_fd;
animation->delay = gdk_pixbuf_animation_iter_get_delay_time(animation->iter);
animation->valid = TRUE;

layout_image_animate_update_image(animation->lw);
layout_image_animate_update_image(animation->lw);

g_timeout_add(animation->delay, show_next_frame, animation);
}
g_timeout_add(animation->delay, show_next_frame, animation);
}
}
else
{
log_printf("Error reading GIF file: %s\n", error->message);
}

g_object_unref(animation->in_file);
g_object_unref(animation->gfstream);
}
else
{
log_printf("Error reading GIF file: %s\n", error->message);
}

g_object_unref(animation->in_file);
g_object_unref(animation->gfstream);
}

static gboolean layout_image_animate_new_file(LayoutWindow *lw)
{
GFileInputStream *gfstream;
GError *error = nullptr;
AnimationData *animation;
GFile *in_file;

Expand All @@ -465,6 +463,7 @@ static gboolean layout_image_animate_new_file(LayoutWindow *lw)

in_file = g_file_new_for_path(lw->image->image_fd->path);
animation->in_file = in_file;
g_autoptr(GError) error = nullptr;
gfstream = g_file_read(in_file, nullptr, &error);
if (gfstream)
{
Expand Down
19 changes: 5 additions & 14 deletions src/layout-util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -986,13 +986,12 @@ static void open_with_response_cb(GtkDialog *, gint response_id, gpointer data)

if (response_id == GTK_RESPONSE_OK)
{
GError *error = nullptr;
g_autoptr(GError) error = nullptr;
g_app_info_launch(open_with_data->application, open_with_data->g_file_list, nullptr, &error);

if (error)
{
log_printf("Error launching app: %s\n", error->message);
g_error_free(error);
}
}

Expand All @@ -1010,15 +1009,14 @@ static void open_with_application_selected_cb(GtkAppChooserWidget *, GAppInfo *a

static void open_with_application_activated_cb(GtkAppChooserWidget *, GAppInfo *application, gpointer data)
{
GError *error = nullptr;
auto open_with_data = static_cast<OpenWithData *>(data);

g_autoptr(GError) error = nullptr;
g_app_info_launch(application, open_with_data->g_file_list, nullptr, &error);

if (error)
{
log_printf("Error launching app.: %s\n", error->message);
g_error_free(error);
}

open_with_data_free(open_with_data);
Expand Down Expand Up @@ -2854,7 +2852,6 @@ static void layout_actions_setup_mark(LayoutWindow *lw, gint mark, const gchar *
static void layout_actions_setup_marks(LayoutWindow *lw)
{
gint mark;
GError *error;
GString *desc = g_string_new(
"<ui>"
" <menubar name='MainMenu'>");
Expand Down Expand Up @@ -2916,11 +2913,10 @@ static void layout_actions_setup_marks(LayoutWindow *lw)
}
g_string_append(desc, "</ui>" );

error = nullptr;
g_autoptr(GError) error = nullptr;
if (!gq_gtk_ui_manager_add_ui_from_string(lw->ui_manager, desc->str, -1, &error))
{
g_message("building menus failed: %s", error->message);
g_error_free(error);
exit(EXIT_FAILURE);
}
g_string_free(desc, TRUE);
Expand Down Expand Up @@ -3005,7 +3001,6 @@ static void layout_actions_editor_add(GString *desc, GList *path, GList *old_pat

static void layout_actions_setup_editors(LayoutWindow *lw)
{
GError *error;
GList *editors_list;
GList *work;
GList *old_path;
Expand Down Expand Up @@ -3101,13 +3096,12 @@ static void layout_actions_setup_editors(LayoutWindow *lw)
g_string_append(desc," </menubar>"
"</ui>" );

error = nullptr;
g_autoptr(GError) error = nullptr;

lw->ui_editors_id = gq_gtk_ui_manager_add_ui_from_string(lw->ui_manager, desc->str, -1, &error);
if (!lw->ui_editors_id)
{
g_message("building menus failed: %s", error->message);
g_error_free(error);
exit(EXIT_FAILURE);
}
g_string_free(desc, TRUE);
Expand All @@ -3128,8 +3122,6 @@ void create_toolbars(LayoutWindow *lw)

void layout_actions_setup(LayoutWindow *lw)
{
GError *error;

DEBUG_1("%s layout_actions_setup: start", get_exec_time());
if (lw->ui_manager) return;

Expand Down Expand Up @@ -3171,12 +3163,11 @@ void layout_actions_setup(LayoutWindow *lw)
gq_gtk_ui_manager_insert_action_group(lw->ui_manager, lw->action_group, 0);

DEBUG_1("%s layout_actions_setup: add menu", get_exec_time());
error = nullptr;
g_autoptr(GError) error = nullptr;

if (!gq_gtk_ui_manager_add_ui_from_resource(lw->ui_manager, options->hamburger_menu ? GQ_RESOURCE_PATH_UI "/menu-hamburger.ui" : GQ_RESOURCE_PATH_UI "/menu-classic.ui" , &error))
{
g_message("building menus failed: %s", error->message);
g_error_free(error);
exit(EXIT_FAILURE);
}

Expand Down
3 changes: 1 addition & 2 deletions src/lua.cc
Original file line number Diff line number Diff line change
Expand Up @@ -394,12 +394,11 @@ gchar *lua_callvalue(FileData *fd, const gchar *file, const gchar *function)
}

gchar *data = g_strdup(lua_tostring(L, -1));
GError *error = nullptr;
g_autoptr(GError) error = nullptr;
g_autofree gchar *tmp = g_locale_to_utf8(data, strlen(data), nullptr, nullptr, &error);
if (error)
{
log_printf("Error converting lua output from locale to UTF-8: %s\n", error->message);
g_error_free(error);
}
else
{
Expand Down
Loading

0 comments on commit 41ad5f5

Please sign in to comment.