]> granicus.if.org Git - graphviz/commit
smyrna savefiledlg, on_gvprbuttonsave_clicked: fix memory leaks
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 10 Oct 2022 15:08:51 +0000 (08:08 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 15 Oct 2022 00:32:13 +0000 (17:32 -0700)
commit99cc6ba2ab9d70da3def6833b4fce0cc3f338f75
tree5988f2b02993f3e045876cebbe586bb6f4dbbab3
parente7fb9a1b807a14d39a777fe137eb09adf2bd0329
smyrna savefiledlg, on_gvprbuttonsave_clicked: fix memory leaks

The `gtk_file_chooser_get_filename` docs¹ note:

  Return value
  …
  The currently selected filename, or NULL if no file is selected, or the
  selected file can’t be represented with a local filename. Free with g_free().

  The caller of the method takes ownership of the data, and is responsible for
  freeing it.

Contrary to this, `savefiledlg` was duplicating the pointed to data and then
losing the pointer `gtk_file_chooser_get_filename` returned.

The straightforward fix to this would be to retain the pointer and then `g_free`
it after copying its contents into the buffer `xbuf`. However, we can instead
refactor this function to avoid the copy altogether and simply pass the original
memory back to the caller, making this both a fix and an optimization.

On top of this, `on_gvprbuttonsave_clicked` was never calling `agxbfree` on its
local `xbuf`. By moving to a standard string, we also fix this memory leak.

¹ https://docs.gtk.org/gtk3/method.FileChooser.get_filename.html
cmd/smyrna/gui/gui.c
cmd/smyrna/gui/gui.h
cmd/smyrna/gui/menucallbacks.c