Fix memory leaks
Some checks failed
SwiftLint / SwiftLint (push) Successful in 6s
Deploy Docs / publish (push) Failing after 25s

This commit is contained in:
david-swift 2025-01-23 13:02:11 +01:00
parent a0c0136298
commit 5613ce13cd
9 changed files with 35 additions and 19 deletions

View File

@ -28,9 +28,13 @@ static void
gtui_filedialog_open_finish (uint64_t dialog, uint64_t result, uint64_t data) gtui_filedialog_open_finish (uint64_t dialog, uint64_t result, uint64_t data)
{ {
GFile *file = gtk_file_dialog_open_finish (dialog, result, NULL); GFile *file = gtk_file_dialog_open_finish (dialog, result, NULL);
if (file != NULL) {
const char *path = g_file_peek_path (file); const char *path = g_file_peek_path (file);
g_object_unref (file); g_object_unref (file);
filedialog_on_open_cb (dialog, path, data); filedialog_on_open_cb (dialog, path, data);
} else {
filedialog_on_open_cb (dialog, NULL, data);
}
} }
static void static void

View File

@ -14,11 +14,4 @@ extension Bool {
self ? 1 : 0 self ? 1 : 0
} }
/// The gboolean as a GValue.
public var gValue: UnsafePointer<GValue> {
let pointer = UnsafeMutablePointer<GValue>.allocate(capacity: 1)
pointer.initialize(to: gtui_initialize_boolean(cBool))
return .init(pointer)
}
} }

View File

@ -121,7 +121,7 @@ public struct AboutDialog: AdwaitaWidget {
if storage.content[dialogID]?.first != nil { if storage.content[dialogID]?.first != nil {
let dialog = storage.content[dialogID]?.first?.opaquePointer let dialog = storage.content[dialogID]?.first?.opaquePointer
adw_dialog_close(dialog?.cast()) adw_dialog_close(dialog?.cast())
g_object_unref(dialog?.cast()) storage.content[dialogID] = []
} }
} }
} }

View File

@ -125,6 +125,9 @@ public struct AlertDialog: AdwaitaWidget {
) where Data: ViewRenderData { ) where Data: ViewRenderData {
storage.fields[Self.visibleID + id] = _visible storage.fields[Self.visibleID + id] = _visible
child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type) child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
if let storage = storage.content["extra-child"]?.first {
extraChild?.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
}
guard updateProperties else { guard updateProperties else {
return return
} }
@ -164,7 +167,8 @@ public struct AlertDialog: AdwaitaWidget {
if storage.content[Self.dialogID + id]?.first != nil { if storage.content[Self.dialogID + id]?.first != nil {
let dialog = storage.content[Self.dialogID + id]?.first?.opaquePointer let dialog = storage.content[Self.dialogID + id]?.first?.opaquePointer
adw_dialog_close(dialog?.cast()) adw_dialog_close(dialog?.cast())
g_object_unref(dialog?.cast()) storage.content[Self.dialogID] = []
storage.content["extra-child"] = nil
} }
} }
} }
@ -213,6 +217,7 @@ public struct AlertDialog: AdwaitaWidget {
if let extraChild { if let extraChild {
let child = extraChild.storage(data: data, type: AdwaitaMainView.self) let child = extraChild.storage(data: data, type: AdwaitaMainView.self)
let childPointer = child.pointer as? OpaquePointer let childPointer = child.pointer as? OpaquePointer
storage.content["extra-child"] = [child]
adw_alert_dialog_set_extra_child(pointer?.cast(), childPointer?.cast()) adw_alert_dialog_set_extra_child(pointer?.cast(), childPointer?.cast())
} }
} }

View File

@ -113,6 +113,7 @@ public struct Dialog: AdwaitaWidget {
if storage.content[dialogID + id]?.first != nil { if storage.content[dialogID + id]?.first != nil {
let dialog = storage.content[dialogID + id]?.first?.opaquePointer let dialog = storage.content[dialogID + id]?.first?.opaquePointer
adw_dialog_close(dialog?.cast()) adw_dialog_close(dialog?.cast())
storage.content[dialogID + id] = []
} }
} }
} }

View File

@ -91,7 +91,9 @@ public struct FileDialog: AdwaitaWidget {
} }
child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type) child.updateStorage(storage, data: data, updateProperties: updateProperties, type: type)
if open.update, storage.fields["callbacks"] == nil { if open.update, storage.fields["callbacks"] == nil {
var unref: [OpaquePointer?] = []
let pointer = gtk_file_dialog_new() let pointer = gtk_file_dialog_new()
unref.append(pointer)
if let initialName { if let initialName {
gtk_file_dialog_set_initial_name(pointer, initialName) gtk_file_dialog_set_initial_name(pointer, initialName)
} }
@ -101,18 +103,24 @@ public struct FileDialog: AdwaitaWidget {
gtk_file_filter_add_suffix(filter, name) gtk_file_filter_add_suffix(filter, name)
} }
gtk_file_dialog_set_default_filter(pointer, filter) gtk_file_dialog_set_default_filter(pointer, filter)
g_object_unref(filter?.cast()) unref.append(filter)
} else { } else {
gtk_file_dialog_set_default_filter(pointer, nil) gtk_file_dialog_set_default_filter(pointer, nil)
} }
if let initialFolder { if let initialFolder {
let file = g_file_new_for_path(initialFolder.absoluteString) let file = g_file_new_for_path(initialFolder.absoluteString)
gtk_file_dialog_set_initial_folder(pointer, file) gtk_file_dialog_set_initial_folder(pointer, file)
g_object_unref(file?.cast()) unref.append(file)
} }
let callbacks = AdwaitaFileDialog() let callbacks = AdwaitaFileDialog()
callbacks.onResult = { (storage.fields["result"] as? (URL) -> Void)?($0); g_object_unref(pointer?.cast()) } let unrefClosure = {
callbacks.onCancel = { (storage.fields["cancel"] as? () -> Void)?() } for ref in unref {
g_object_unref(ref?.cast())
}
storage.fields["callbacks"] = nil
}
callbacks.onResult = { (storage.fields["result"] as? (URL) -> Void)?($0); unrefClosure() }
callbacks.onCancel = { (storage.fields["cancel"] as? () -> Void)?(); unrefClosure() }
callbacks.reset = { storage.fields["callbacks"] = nil } callbacks.reset = { storage.fields["callbacks"] = nil }
storage.fields["callbacks"] = callbacks storage.fields["callbacks"] = callbacks
let ptr = UInt64(Int(bitPattern: pointer)) let ptr = UInt64(Int(bitPattern: pointer))
@ -138,8 +146,9 @@ class AdwaitaFileDialog {
var reset: () -> Void = { } var reset: () -> Void = { }
/// Initialize the window callback. /// Initialize the window callback.
init() { init() { }
}
deinit { print("DEINIT fd") }
/// Run this when a file gets opened. /// Run this when a file gets opened.
/// - Parameter path: The file path. /// - Parameter path: The file path.

View File

@ -38,6 +38,7 @@ extension ComboRow {
let list = gtk_string_list_new(nil) let list = gtk_string_list_new(nil)
storage.fields[Self.stringList] = list storage.fields[Self.stringList] = list
adw_combo_row_set_model(storage.opaquePointer?.cast(), list) adw_combo_row_set_model(storage.opaquePointer?.cast(), list)
g_object_unref(list?.cast())
Self.updateContent(storage: storage, values: values, element: Element.self) Self.updateContent(storage: storage, values: values, element: Element.self)
} }
updateFunctions.append { storage, _, _ in updateFunctions.append { storage, _, _ in

View File

@ -20,6 +20,7 @@ extension Picture {
let pointer = storage.opaquePointer let pointer = storage.opaquePointer
guard let data else { guard let data else {
if storage.fields[oldData] != nil { if storage.fields[oldData] != nil {
g_object_unref(gtk_picture_get_paintable(pointer)?.cast())
gtk_picture_set_paintable(pointer, gdk_paintable_new_empty(0, 0)) gtk_picture_set_paintable(pointer, gdk_paintable_new_empty(0, 0))
storage.fields[oldData] = nil storage.fields[oldData] = nil
} }

View File

@ -24,7 +24,9 @@ struct AlertDialogDemo: View {
.frame(maxWidth: 100) .frame(maxWidth: 100)
.padding() .padding()
} }
.alertDialog(visible: $dialog, heading: "Alert Dialog", body: "This is an alert dialog") .alertDialog(visible: $dialog, heading: "Alert Dialog", body: "This is an alert dialog") {
CounterDemo()
}
.response("Cancel", role: .close) { .response("Cancel", role: .close) {
print("Cancel") print("Cancel")
} }