Page 1 of 1
Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 11:06 am
by jak64
Gadgets and FreeGadget
Hello everyone,
I would like to have your opinion on a question that I ask myself. For this, I will take an example to explain myself. It is just an example, it has nothing to do with what I want to do.
Let's imagine that I write a program, with on the left of the screen, several buttons. A "Trees" button, an "Animals" button, a "Fruits" button, etc...
By clicking, for example, on the "Trees" button, I display pictures of trees in ImageGadget(), there is a quantity that I do not know.
Likewise, if I click on the "Animals" button, I display pictures of animals in ImageGadget().
My idea is to dynamically create the ImageGadet() each time and then free them (Freegadget()).
The question I ask myself is this.
Doesn't creating and freeing (FreeGadget()) risk saturating the computer's memory after a while?
Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 11:10 am
by Fred
No, it shouldn't.
Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 11:13 am
by Bisonte
Create an ImageGadget and use SetGadgetState() to add a new image every time.
This way you do not need to create a new gadget for each image. Use dynamically
created images (or load them with #PB_Any as number) and release them after use.
That would be the normal procedure.
And to the main question:
No, it should have no effect on the total memory of the computer.
The operating system should take care of these things.
Unless you forget to free something

Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 11:51 am
by jak64
Hello Fred and Bisonte,
Thanks for your answers.
To answer Bisonte, of course, I can create the gadgets and reuse them with SetGadgetState(), but I don't know the number of images to display, so I would have to create a certain number of ImageGadget without being sure to have created enough.
Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 11:55 am
by BarryG
jak64 wrote: Mon Mar 10, 2025 11:06 amDoesn't creating and freeing (FreeGadget()) risk saturating the computer's memory after a while?
No; in fact that's the total opposite. Freeing it before recreating is the best way to not saturate your PC's memory.
Fun Fact: You don't even need to free a gadget if you recreate it with the same gadget number, because PureBasic will free it automatically.
Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 1:19 pm
by jak64
Hello BarryG
Thanks, I didn't know that recreating it with the same number automatically frees it, but in my case I create them with #pb_Any, and as I mentioned, I don't know the number to create (100, 200, 500, 1000?)
Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 2:14 pm
by mk-soft
With #PB_Any you must explicitly call FreeGadget.
Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 4:40 pm
by jak64
thanks mk-soft
Re: Gadgets and FreeGadget
Posted: Mon Mar 10, 2025 10:28 pm
by mk-soft
Alternatively, create a CanvasGadget yourself and draw a grid with the images.
Display the CanvasGadget in the size of the window and mathematically redraw only the images that fit into the window.
Re: Gadgets and FreeGadget
Posted: Tue Mar 11, 2025 1:30 pm
by jak64
Hello mk-soft,
I opted for a scrollarea in which I add as many gadgets as necessary and it works very well like that.
However, I just made another post, because I noticed that, if we use a scrollarea in which we put canvasgadgets, then the mouse wheel does not work to scroll vertically!
Re: Gadgets and FreeGadget
Posted: Tue Mar 11, 2025 3:21 pm
by Axolotl
@jak64,
As I showed in the other post, the behavior is logical and understandable, because both gadgets can receive the mouse wheel event.
Re: Gadgets and FreeGadget
Posted: Tue Mar 11, 2025 4:00 pm
by jak64
OK, Thanks Axolotl