Gadgets and FreeGadget

Just starting out? Need help? Post your questions and find answers here.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Gadgets and FreeGadget

Post 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?
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Gadgets and FreeGadget

Post by Fred »

No, it shouldn't.
User avatar
Bisonte
Addict
Addict
Posts: 1320
Joined: Tue Oct 09, 2007 2:15 am

Re: Gadgets and FreeGadget

Post 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 ;)
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Gadgets and FreeGadget

Post 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.
BarryG
Addict
Addict
Posts: 4219
Joined: Thu Apr 18, 2019 8:17 am

Re: Gadgets and FreeGadget

Post 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.
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Gadgets and FreeGadget

Post 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?)
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Gadgets and FreeGadget

Post by mk-soft »

With #PB_Any you must explicitly call FreeGadget.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Gadgets and FreeGadget

Post by jak64 »

thanks mk-soft
User avatar
mk-soft
Always Here
Always Here
Posts: 6315
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Gadgets and FreeGadget

Post 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.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Gadgets and FreeGadget

Post 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!
Axolotl
Addict
Addict
Posts: 872
Joined: Wed Dec 31, 2008 3:36 pm

Re: Gadgets and FreeGadget

Post 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.
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
jak64
Enthusiast
Enthusiast
Posts: 639
Joined: Sat Aug 15, 2020 5:02 pm
Location: Ciboure (France)

Re: Gadgets and FreeGadget

Post by jak64 »

OK, Thanks Axolotl
Post Reply