Skip to content

Commit

Permalink
fix(application): fix file loop and use of GTK object after lifecycle
Browse files Browse the repository at this point in the history
Swappy tried to access Gtk data after the end of the mainloop and the
file save routine had infinite recursion
  • Loading branch information
pathetic-lynx authored and jtheoof committed Feb 18, 2020
1 parent f4e9a19 commit 320dae0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
12 changes: 9 additions & 3 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,13 @@ static void save_state_to_file_or_folder(struct swappy_state *state,
g_object_unref(pixbuf);
}

void on_destroy(GtkApplication *application, gpointer data) {
struct swappy_state *state = (struct swappy_state *)data;
if (state->output_file != NULL) {
save_state_to_file_or_folder(state, state->output_file);
}
}

void brush_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
switch_mode_to_brush(state);
}
Expand All @@ -200,9 +207,6 @@ void arrow_clicked_handler(GtkWidget *widget, struct swappy_state *state) {
}

void application_finish(struct swappy_state *state) {
if (state->output_file != NULL) {
save_state_to_file_or_folder(state, state->output_file);
}
paint_free_all(state);
buffer_free_all(state);
cairo_surface_destroy(state->cairo_surface);
Expand Down Expand Up @@ -519,6 +523,8 @@ static bool load_layout(struct swappy_state *state) {
GtkWindow *window =
GTK_WINDOW(gtk_builder_get_object(builder, "paint-window"));

g_signal_connect(window, "destroy", G_CALLBACK(on_destroy), state);

state->ui->undo = GTK_BUTTON(gtk_builder_get_object(builder, "undo-button"));
state->ui->redo = GTK_BUTTON(gtk_builder_get_object(builder, "redo-button"));

Expand Down
29 changes: 16 additions & 13 deletions src/pixbuf.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,8 @@ GdkPixbuf *pixbuf_get_from_state(struct swappy_state *state) {
return pixbuf;
}

void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder) {
static void write_file(GdkPixbuf *pixbuf, char *path) {
GError *error = NULL;

time_t current_time;
char *c_time_string;

time(&current_time);

c_time_string = ctime(&current_time);
c_time_string[strlen(c_time_string) - 1] = '\0';
char path[MAX_PATH];
snprintf(path, MAX_PATH, "%s/%s %s.png", folder, "Swappshot", c_time_string);
gdk_pixbuf_savev(pixbuf, path, "png", NULL, NULL, &error);

if (error != NULL) {
Expand All @@ -40,6 +30,19 @@ void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder) {
g_free(message);
}

void pixbuf_save_state_to_folder(GdkPixbuf *pixbuf, char *folder) {
time_t current_time;
char *c_time_string;

time(&current_time);

c_time_string = ctime(&current_time);
c_time_string[strlen(c_time_string) - 1] = '\0';
char path[MAX_PATH];
snprintf(path, MAX_PATH, "%s/%s %s.png", folder, "Swappshot", c_time_string);
write_file(pixbuf, path);
}

void pixbuf_save_to_stdout(GdkPixbuf *pixbuf) {
GOutputStream *out;
GError *error = NULL;
Expand All @@ -61,6 +64,6 @@ void pixbuf_save_to_file(GdkPixbuf *pixbuf, char *file) {
if (g_strcmp0(file, "-") == 0) {
pixbuf_save_to_stdout(pixbuf);
} else {
pixbuf_save_to_file(pixbuf, file);
write_file(pixbuf, file);
}
}
}

0 comments on commit 320dae0

Please sign in to comment.