Is it safe to delete an ImageGadget image after setting it?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Is it safe to delete an ImageGadget image after setting it?

Post by Mistrel »

Are there any issues deleting an image used by ImageGadget() and ButtonImageGadget()? Or it been blitted elsewhere and the original image is safe to release?

I know that a modified image will not display any changes until a new call to SetGadgetState() is made, even if it's to the same image.

I wrote a demo application which mimics my use case; I have a pre-rendered image which I copy and modify every time I want to update an image gadget. Once the gadget has been updated, I no longer need the modified image. Is it OK to just free the image immediately after setting it to the gadget or are there any issues with this that I just haven't encountered?

THE FOLLOWING CODE HAS MULTIPLE COLORS BLINKING AT 200MS (5-times a second). CHANGE IT TO SOMETHING SLOWER IF YOU THINK THIS MIGHT TRIGGER A SEIZURE

I tested this only on Windows 10.

Example:

Code: Select all

Procedure drawPattern(imageGadget, image, width)
  imageCopy=GrabImage(image,#PB_Any,0,0,132,103)
  
  StartDrawing(ImageOutput(imageCopy))
  DrawingMode(#PB_2DDrawing_AllChannels)
  
  For x=0 To width/2-5 Step 10
    Box(x,y,width-2*x,width-2*y,RGBA(Random(255),Random(255),Random(255),255))
    y+10
  Next x
  
  StopDrawing()
  
  SetGadgetState(imageGadget,ImageID(imageCopy))
  
  FreeImage(imageCopy)
EndProcedure

window=OpenWindow(#PB_Any,0,0,132,136,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
image=CreateImage(#PB_Any,132,103,32,#PB_Image_Transparent)
imageGadget=ImageGadget(#PB_Any,16,5,132,103,ImageID(image))
buttonGadget=ButtonGadget(#PB_Any,5,109,122,23,"Close")
width=100

drawPattern(imageGadget,image,width)

start=ElapsedMilliseconds()

Repeat
  event=WaitWindowEvent(1)
  
  If ElapsedMilliseconds()-start>200
    drawPattern(imageGadget,image,width)
    
    start=ElapsedMilliseconds()
  EndIf
  
  If event=#PB_Event_Gadget And EventGadget()=buttonGadget
    End
  EndIf
Until event=#PB_Event_CloseWindow
WilliamL
Addict
Addict
Posts: 1224
Joined: Mon Aug 04, 2008 10:56 pm
Location: Seattle, USA

Re: Is it safe to delete an ImageGadget image after setting

Post by WilliamL »

That is a question that I had too and, after a search, I ended up here with your question (avoiding the embarrassment of repeating the question).

I'm doing pretty much the same thing in a small GIF program where the window can be resized and by doing that the image is resized. I'm copying the original image and resizing it and using this copy for the image gadget. I'm using the same constants for the original image and the resized image and I wondered if 'FreeImage' should be used on the copy after rendering it.

I'm guessing that if I use the same constants for the images there wouldn't be a problem but if I used #PB_Any that the memory would grow each time I used it unless I used 'FreeImage'. I think I asked the same question a few years ago and in that case I learned that when you use 'CreateImage' it frees the image of the same number automatically. (if that helps)

...maybe a little guide of how/when to use FreeImage is needed.

PS Your program ran fine on my Mac
MacBook Pro-M1 (2021), Sonoma 14.4.1, PB 6.10LTS M1
Post Reply