Skip to content

Commit 7c196e2

Browse files
author
Sylphrena
committed
Improved examples to have more respectable memory management
1 parent 8976900 commit 7c196e2

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

examples/widget-template-manual/window.odin

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ my_box_get_type :: proc "c" () -> (g_type: gobj.Type) {
8787
context = runtime.default_context()
8888
fmt.printfln("Button clicked %d times!", my_box.button_clicked)
8989

90-
button_clicked_cstring := fmt.ctprintf("Clicked %d times!", my_box.button_clicked)
90+
// GTK copies our string, so we are responsible for freeing the memory.
91+
button_clicked_cstring := fmt.caprintf("Clicked %d times!", my_box.button_clicked)
92+
defer delete(button_clicked_cstring)
93+
9194
gtk.button_set_label(button, button_clicked_cstring)
9295
}
9396
}

examples/widget-template-with-helper/window.odin

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ my_button_clicked :: proc "c"(button: ^gtk.Button, data: glib.pointer) {
132132
context = runtime.default_context()
133133
fmt.printfln("Button clicked %d times!", my_box.button_clicked)
134134

135-
button_clicked_cstring := fmt.ctprintf("Clicked %d times!", my_box.button_clicked)
135+
button_clicked_cstring := fmt.caprintf("Clicked %d times!", my_box.button_clicked)
136136

137137
// We set the property of our box, and tell GTK that it changed, which will automatically update everything that depends on it.
138+
delete(my_box.button_label)
138139
my_box.button_label = button_clicked_cstring
139140
gobj.object_notify(cast(^gobj.Object)my_box, "button-label")
140141

gtk/template_helper.odin

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ register_type :: proc(
5858
instance_init = instance_init_proc,
5959
}
6060

61-
my_type_name := fmt.ctprint(typeid_of(instance_type))
61+
my_type_name := fmt.caprint(typeid_of(instance_type))
62+
defer delete(my_type_name)
6263
registered_g_type := gobj.type_register_static(parent_g_type, my_type_name, &info, .NONE)
6364

6465
static_g_type_ptr^ = registered_g_type
@@ -105,7 +106,9 @@ register_type_with_template :: proc(
105106
instance_init = instance_init_proc,
106107
}
107108

108-
my_type_name := fmt.ctprint(typeid_of(instance_type))
109+
my_type_name := fmt.caprint(typeid_of(instance_type))
110+
defer delete(my_type_name)
111+
109112
registered_g_type := gobj.type_register_static(parent_g_type, my_type_name, &info, .NONE)
110113

111114
static_g_type_ptr^ = registered_g_type
@@ -192,7 +195,9 @@ class_init_default_template :: proc "c" (class: glib.pointer, data: glib.pointer
192195

193196
create_param_spec :: proc(type: typeid, tag: string) -> (param_spec: ^gobj.ParamSpec, ok: bool) {
194197
ok = true
195-
name := fmt.ctprint(tag)
198+
name := fmt.caprint(tag)
199+
defer delete(name)
200+
196201
switch type {
197202
case string:
198203
param_spec = gobj.param_spec_string(

0 commit comments

Comments
 (0)