Skip to content

Commit

Permalink
Free GError
Browse files Browse the repository at this point in the history
  • Loading branch information
can-lehmann committed Mar 28, 2024
1 parent fffc367 commit bae4a0e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions owlkettle.nim
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ proc sendNotification*(id, title, body: string,
var err = GError(nil)
let gIcon = g_icon_new_for_string(icon.cstring, err.addr)
if not err.isNil:
g_error_free(err)
raise newException(IoError, "Icon \"" & icon & "\" is unknown")
g_notification_set_icon(notification, gIcon)
g_object_unref(pointer(gIcon))
Expand Down
5 changes: 4 additions & 1 deletion owlkettle/bindings/gtk.nim
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,9 @@ proc `=sink`*(dest: var OwnedGtkString; source: OwnedGtkString) =
proc `$`*(cStr: OwnedGtkString): string = $(cStr.cstring)

{.push importc, cdecl.}
# GLib.Error
proc g_error_free*(error: GError)

# GLib.Source
proc g_source_remove*(id: cuint): cbool

Expand Down Expand Up @@ -672,7 +675,7 @@ proc gtk_widget_measure*(widget: GtkWidget, orient: GtkOrientation, size: cint,

# Gtk.CssProvider
proc gtk_css_provider_new*(): GtkCssProvider
proc gtk_css_provider_load_from_path*(cssProvider: GtkCssProvider, path: cstring, error: ptr GError): cbool
proc gtk_css_provider_load_from_path*(cssProvider: GtkCssProvider, path: cstring)
proc gtk_css_provider_load_from_data*(cssProvider: GtkCssProvider, data: cstring, length: csize_t)

# Gtk.StyleContext
Expand Down
5 changes: 1 addition & 4 deletions owlkettle/mainloop.nim
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,8 @@ proc newStylesheet*(css: string, priority: int = DEFAULT_PRIORITY): Stylesheet =

proc loadStylesheet*(path: string, priority: int = DEFAULT_PRIORITY): Stylesheet =
## Loads a CSS stylesheet from the given path
var error: GError
let provider = gtk_css_provider_new()
discard gtk_css_provider_load_from_path(provider, path.cstring, error.addr)
if not error.isNil:
raise newException(IOError, $error[].message)
gtk_css_provider_load_from_path(provider, path.cstring)
result = Stylesheet(provider: provider, priority: priority)

type
Expand Down
14 changes: 12 additions & 2 deletions owlkettle/widgets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,7 @@ proc loadPixbuf*(path: string): Pixbuf =
let pixbuf = gdk_pixbuf_new_from_file(path.cstring, error.addr)
if not error.isNil:
let message = $error[].message
g_error_free(error)
raise newException(IoError, "Unable to load pixbuf: " & message)

result = newPixbuf(pixbuf)
Expand All @@ -706,6 +707,7 @@ proc loadPixbuf*(path: string,
)
if not error.isNil:
let message = $error[].message
g_error_free(error)
raise newException(IoError, "Unable to load pixbuf: " & message)

result = newPixbuf(pixbuf)
Expand All @@ -716,6 +718,7 @@ proc openInputStream(path: string): GInputStream =
result = g_file_read(file, nil, error.addr)
if not error.isNil:
let message = $error[].message
g_error_free(error)
raise newException(IoError, "Unable to load pixbuf: " & message)

proc handlePixbufReady(stream: pointer, result: GAsyncResult, data: pointer) {.cdecl.} =
Expand All @@ -727,13 +730,15 @@ proc handlePixbufReady(stream: pointer, result: GAsyncResult, data: pointer) {.c
future.complete(newPixbuf(pixbuf))
else:
let message = $error[].message
g_error_free(error)
future.fail(newException(IoError, "Unable to load pixbuf: " & message))

if not stream.isNil:
var error = GError(nil)
discard g_input_stream_close(GInputStream(stream), nil, error.addr)
if not error.isNil:
let message = $error[].message
g_error_free(error)
raise newException(IoError, "Unable to close stream: " & message)
g_object_unref(stream)

Expand Down Expand Up @@ -851,6 +856,7 @@ proc save*(pixbuf: Pixbuf,
)
if not error.isNil:
let message = $error[].message
g_error_free(error)
raise newException(IoError, "Unable to save pixbuf: " & message)

type ContentFit* = enum
Expand Down Expand Up @@ -2145,8 +2151,12 @@ renderable ModelButton of BaseWidget:
g_object_set_property(state.internalWidget.pointer, "iconic", value.addr)
g_value_unset(value.addr)
if state.icon.len > 0:
var err: GError
let icon = g_icon_new_for_string(state.icon.cstring, err.addr)
var error = GError(nil)
let icon = g_icon_new_for_string(state.icon.cstring, error.addr)
if not error.isNil:
let message = $error[].message
g_error_free(error)
raise newException(IoError, "Unable to load icon: " & message)
var value = g_value_new(icon)
g_object_set_property(state.internalWidget.pointer, "icon", value.addr)
g_value_unset(value.addr)
Expand Down

0 comments on commit bae4a0e

Please sign in to comment.