@Fred? Is it okay to re-create gadgets (using FreeGadget())?

Everything else that doesn't fall into one of the other PB categories.
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

@Fred? Is it okay to re-create gadgets (using FreeGadget())?

Post by Kukulkan »

Hi,

I wonder if it is okay to re-create gadgets with the same ID? Some code will show best:

Code: Select all

Procedure main()
  Protected win.i = OpenWindow(0, 0, 0, 600, 300, "Test", 
                               #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  If win.i
    Protected button.i = ButtonGadget(#PB_Any, 10, 170, 200, 20, "Button")
    Protected start.i = ElapsedMilliseconds()
    
    ; Re-Create the Gadget every 5 seconds
    Repeat
      If ElapsedMilliseconds() > start.i + 5000
        FreeGadget(button.i)
        If ButtonGadget(button.i, 10, 170, 200, 20, "Button") = 0
          Debug "FAILED: New Gadget with ID " + Str(button.i) + " failed!"
        Else
          Debug "OKAY: New Gadget with ID " + Str(button.i) + " created."
        EndIf
        start.i = ElapsedMilliseconds()
      EndIf
    Until WaitWindowEvent(1) = #PB_Event_CloseWindow 
  EndIf
EndProcedure

main()
The debugger complains about high gadget-id, but if I continue it works. Without debugger, it also seem to work.

Is this allowed?

If not, why? If FreeGadget() was successful, there should be no problem, right?

If yes, are the resources freed and there are really no drawbacks? Is this for all gadgets and all OS? Including WebGadget?
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by STARGÅTE »

Kukulkan wrote:Is this allowed?
Yes!
Kukulkan wrote:If yes, are the resources freed and there are really no drawbacks? Is this for all gadgets and all OS? Including WebGadget?
The most important drawbacks is, that (if you create a Gadget with ID 100000 by hand) you initialize an array (for all possible numbers) with the size of 100000 * 8 Byte (Integer), but the whole array ist mostly empty.

Look at this:

Code: Select all

Procedure.q MemoryInUse()
	
	ProcedureReturn MemoryStatus(#PB_System_TotalPhysical)-MemoryStatus(#PB_System_FreePhysical)
	
EndProcedure

OpenConsole()
OpenWindow(0, 0, 0, 600, 300, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

Define Begin.q = MemoryInUse()

Define Button = ButtonGadget(#PB_Any, 10, 170, 200, 20, "Button")

PrintN("nearly no memory used: "+StrF((MemoryInUse()-Begin)/1000000.,2)+" MB")

FreeGadget(Button)
ButtonGadget(Button, 10, 170, 200, 20, "Button")

PrintN("gadget number: "+Str(Button))
PrintN("high amount of memory used: "+StrF((MemoryInUse()-Begin)/1000000.,2)+" MB")

Debug MemoryInUse()-Begin

Input()

End
For example:

Code: Select all

nearly no memory used: 0.00 MB
gadget number: 30753040
high amount of memory used: 243.67 MB
It is therefore not recommended, to use high-number IDs (for all PB libraries)
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
HeX0R
Addict
Addict
Posts: 979
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by HeX0R »

I guess you might run into trouble when using x86, because then it could happen that "button" gets negative.
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by Little John »

@Kukulkan:
I think mixing both coding styles like in your code is not a good idea.

Either use always

Code: Select all

button.i = ButtonGadget(#PB_Any, ...)
or always

Code: Select all

ButtonGadget(MyNumber.i, ...)
This will avoid the problems that have been mentioned by others here.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by mk-soft »

Why not use both methods...
For static single window use constants and dynamic multiple window with structured data and #PB_Any
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
Little John
Addict
Addict
Posts: 4519
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by Little John »

mk-soft wrote:Why not use both methods...
For static single window use constants and dynamic multiple window with structured data and #PB_Any
What I wrote above was not a general statement about programming in PureBasic, but a specific comment on the code that Kukulkan has posted here:
Little John wrote:I think mixing both coding styles like in your code is not a good idea.
And I also explained the reason why.

Both methods can be used independently from each other. But it's not good to intermix them like Kukulkan did.
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by Kukulkan »

Hi,

I normally always use #PB_Any and do not like static numbers at all. This is a demo to explain my specific question. I did a new gadget with #PB_Any. And then I like to remove it and re-create it with the same ID. This is why I use a fixed number then, causing a mixed use.

And this raised the question, if this is allowed. As I'm also on 32 bit (Windows, others all 64 Bit), it might be that the id is negative and PB then denies the re-creation with this number? This is odd... Also, I don't know why the debugger complains about an ID > 10000. Is there any good reason for that?

Don't get me wrong, this is just an idea for a workaround, if another problem with WebGadget does not get solved. It is about missing resize. Some re-creation of the gadget, after resize of the window, seem the only solution to me. I can also do a new number using #PB_Any, but it would save me some lines of code if I can simply re-use the number.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by NicTheQuick »

Kukulkan wrote:This is odd... Also, I don't know why the debugger complains about an ID > 10000. Is there any good reason for that?
There is a good reason for that. Re-read STARGÅTE's post.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by Kukulkan »

Hi Nic,

Memory consumption? But if I use #PB_Any, the array will become much bigger anyway? And the Gadget-ID I want to re-use is already given so no change for the array? The memory is already taken by #PB_Any generated number.

Maybe I'm wrong. Sorry, I don't get it.
User avatar
STARGÅTE
Addict
Addict
Posts: 2067
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by STARGÅTE »

Kukulkan wrote:Memory consumption? But if I use #PB_Any, the array will become much bigger anyway? And the Gadget-ID I want to re-use is already given so no change for the array? The memory is already taken by #PB_Any generated number.
No.

If you use #PB_Any as "ID", the number wich is return is (simplified) the direct address to the internal element structure for the gadget, it is like "ID = AddElement()" and no array is needed.
If you use a number (or an old ID from #PB_Any) as "ID", the internal element structure is also created, but not on the same plance. Further more, the address is stored in an array, you get a fast access. This is like saving the pointer from AddElement in an array like "FastAccess(ID) = AddElement()".
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by mk-soft »

I thought it was clear that the use of constants (array) and #PB_Any (dynamic) is processed differently from Purebasic.

So when using the #PB_Any method you have to delete the gadget itself (FreeGadget(Gadget Variable)) and then recreate it with #PB_Any and save the result back into the gadget variable.

If you want to renew the gadget in a procedure, you can also pass the pointer to the gadget variable...

Code: Select all

Procedure RenewGadget(*gadget.Integer, Parm1, Param2, ...)
  If IsGadget(*gadget\i)
    FreeGadget(*gadget\i)
    *gadget\i = StringGadget(...)
  EndIf
EndProcedure

gadget = StringGadget(...)
RenewGadget(@gadget, ...)
I find constants to use the windows, gadgets, etc not so bad.
Especially if you write tools for yourself that automate these constants.

I use these procedures for example in my EventDesigner which automatically summarizes all constants and creates the required procedures from the constant name.
Since constants are a consecutive numbering, you can also use them to call the procedures via virtual tables.
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
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by Kukulkan »

Okay, I now understood that setting a gadget id is very different from #PB_Any. A given id is stored and handled by PB in a list (array or whatever storing handle etc). If using #PB_Any, the handle is returned directly and PB does not need to handle. Okay. So I understand the limitation, too.

So I will take the extra effort to re-create a new id using #PB_Any.

Thank you all for the explanations and clarification!

Most important for me is, that I can release (free) a gadget and re-create with no other drawbacks.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: @Fred? Is it okay to re-create gadgets (using FreeGadget

Post by mk-soft »

Kukulkan wrote: ... If using #PB_Any, the handle is returned directly and PB does not need to handle
The handle's not really interpreted correctly.

With #PB_Any you get a dynamic PB Gadget Ident back.
To get the OS specific handle, you have to call the function GadgetID...

Just to clarify the difference between the handle and PB Gadget Ident.
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
Post Reply