fix reference counting in UiGeneric set/get functions

Mon, 09 Jun 2025 08:47:57 +0200

author
Olaf Winermann <olaf.wintermann@gmail.com>
date
Mon, 09 Jun 2025 08:47:57 +0200
changeset 612
8570430391b3
parent 611
a5b393e766c4
child 613
dac25dd922a2

fix reference counting in UiGeneric set/get functions

ui/common/types.c file | annotate | diff | comparison | revisions
ui/ui/toolkit.h file | annotate | diff | comparison | revisions
--- a/ui/common/types.c	Mon Jun 09 08:41:16 2025 +0200
+++ b/ui/common/types.c	Mon Jun 09 08:47:57 2025 +0200
@@ -396,17 +396,29 @@
     }
 }
 
-void ui_image_set(UiGeneric *g, void *img) {
-    // TODO: ref?
+char* ui_text_get(UiText* s) {
+    if (s) {
+        return s->get ? s->get(s) : s->value.ptr;
+    }
+    else {
+        return 0;
+    }
+}
+
+void ui_generic_set_image(UiGeneric *g, void *img) {
     if(g->set) {
         g->set(g, img, UI_IMAGE_OBJECT_TYPE);
     } else {
+        if(g->value) {
+            ui_image_unref(g->value);
+        }
+        ui_image_ref(img);
         g->value = img;
         g->type = UI_IMAGE_OBJECT_TYPE;
     }
 }
 
-void* ui_image_get(UiGeneric *g) {
+void* ui_generic_get_image(UiGeneric *g) {
     if(g->type) {
         if(!strcmp(g->type, UI_IMAGE_OBJECT_TYPE)) {
             return g->value;
@@ -418,16 +430,6 @@
 }
 
 
-char* ui_text_get(UiText* s) {
-    if (s) {
-        return s->get ? s->get(s) : s->value.ptr;
-    }
-    else {
-        return 0;
-    }
-}
-
-
 // private functions
 void uic_int_copy(UiInteger *from, UiInteger *to) {
     to->get = from->get;
--- a/ui/ui/toolkit.h	Mon Jun 09 08:41:16 2025 +0200
+++ b/ui/ui/toolkit.h	Mon Jun 09 08:47:57 2025 +0200
@@ -558,8 +558,8 @@
 UIEXPORT char* ui_string_get(UiString *s);
 UIEXPORT void ui_text_set(UiText *s, const char* value);
 UIEXPORT char* ui_text_get(UiText *s);
-UIEXPORT void ui_image_set(UiGeneric *g, void *img);
-UIEXPORT void* ui_image_get(UiGeneric *g);
+UIEXPORT void ui_generic_set_image(UiGeneric *g, void *img);
+UIEXPORT void* ui_generic_get_image(UiGeneric *g);
 
 UIEXPORT void ui_var_set_int(UiContext *ctx, const char *name, int64_t value);
 UIEXPORT int64_t ui_var_get_int(UiContext *ctx, const char *name);

mercurial