ButtonImageGadget - SetGadgetAttribute doesn't copy image
Posted: Sun Feb 16, 2020 6:41 am
When setting the image of a ButtonImageGadget via SetGadgetAttribute, the underlying image is not copied into the gadget, but instead just the handle to the image is saved in the gadget. This means if you LoadImage an image, you can't call FreeImage on it after setting it in the ButtonImageGadget.
The code below verifies this. In my application (the larger one, not the repro snippet below) I have a thread that repeatedly posts an event to change the image on a ButtonImageGadget, and I think I may have a memory leak because I never free the previous image before loading/setting the next one.
Just wondering if this is by design. "This" meaning that the full image isn't copied into the ButtonImageGadget, just the handle to an image. Perfectly fine and dandy if it's by design, just looking for confirmation.
The code below verifies this. In my application (the larger one, not the repro snippet below) I have a thread that repeatedly posts an event to change the image on a ButtonImageGadget, and I think I may have a memory leak because I never free the previous image before loading/setting the next one.
Just wondering if this is by design. "This" meaning that the full image isn't copied into the ButtonImageGadget, just the handle to an image. Perfectly fine and dandy if it's by design, just looking for confirmation.
Code: Select all
Global btn.i
UseJPEGImageDecoder()
Procedure ImageTest()
Protected img.i
img = LoadImage(#PB_Any, "<path to any valid jpg>")
If IsImage(img)
ResizeImage(img, 100, 100, #PB_Image_Raw)
SetGadgetAttribute(btn, #PB_Button_Image, ImageID(img))
;FreeImage(img) ;Uncomment this line to see issue
EndIf
EndProcedure
OpenWindow(0, 0, 0, 120, 120, "ImageTest", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
btn = ButtonImageGadget(#PB_Any, 10, 10, 100, 100, 0)
ImageTest()
;if FreeImage line is uncommented, hovering over buttonimagegadget displays blank button
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow