Simple question on FreeGadget()

Just starting out? Need help? Post your questions and find answers here.
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Simple question on FreeGadget()

Post by sverson »

Hi there.

May be already answered somewhere in this forum - but did not find it yet.

:?: Does FreeGadget on a ContainerGadget definitely free all gadgets inside the container - even containers in containers in containers etc...?
:?: If there is a complex structure of hundreds of gadgets in a container does just one FreeGadget on this 'master' container remove all gadgets and free all memory used by this gadgets?

In my little test it seems to...

Code: Select all

Structure MyGadget
  Number.i
  ID.i
EndStructure

Structure AllMyGadgets
  HeaderInfo$
  SomeOtherInfo$
  List GadgetList.MyGadget()
EndStructure

Global MyGadgets.AllMyGadgets

Procedure.q AvailPhysMem() ; - Get available physical memory
  Protected MemSize.q = 0
  Protected memex.MEMORYSTATUSEX
  memex\dwLength = SizeOf(MEMORYSTATUSEX)
  GlobalMemoryStatusEx_(@memex)
  MemSize = memex\ullAvailPhys
  ProcedureReturn MemSize
EndProcedure

Procedure CreateGadgets() ; create 5051 crazy gadgets
  With MyGadgets
    If Not ListSize(\GadgetList())
      AddElement(\GadgetList())
      \GadgetList()\Number = ContainerGadget(#PB_Any,0,0,400,400,#PB_Container_BorderLess)
        \GadgetList()\ID = GadgetID(\GadgetList()\Number)
        For X.i = 0 To 49
          AddElement(\GadgetList())
          \GadgetList()\Number = ContainerGadget(#PB_Any,0,0,400,400,#PB_Container_BorderLess)
            \GadgetList()\ID = GadgetID(\GadgetList()\Number)
            SetGadgetColor(\GadgetList()\Number, #PB_Gadget_BackColor, Random($FFFFFF))
            For Y.i = 0 To 49
              AddElement(\GadgetList())
              \GadgetList()\Number = ContainerGadget(#PB_Any,Y<<3,Y<<3,8,8,#PB_Container_BorderLess)
                \GadgetList()\ID = GadgetID(\GadgetList()\Number)
                SetGadgetColor(\GadgetList()\Number, #PB_Gadget_BackColor, Random($FFFFFF))
              CloseGadgetList()
            Next
            For Y.i = 0 To 49
              AddElement(\GadgetList())
              \GadgetList()\Number = StringGadget(#PB_Any,Random(300),Random(379),100,21,"some text")
              \GadgetList()\ID = GadgetID(\GadgetList()\Number)
            Next
          CloseGadgetList()
          SetWindowTitle(0,"MyGadgets "+Str(AvailPhysMem()/$400)+" KB RAM")
          While WindowEvent() : Wend
        Next
      CloseGadgetList()
    EndIf
  EndWith
EndProcedure

Procedure RemoveGadgets()
  With MyGadgets
    If ListSize(\GadgetList())
      FirstElement(\GadgetList())
      FreeGadget(\GadgetList()\Number)
      ClearList(\GadgetList())
    EndIf
  EndWith
  SetWindowTitle(0,"MyGadgets "+Str(AvailPhysMem()/$400)+" KB RAM")
EndProcedure

If OpenWindow(0,0,0,400,420,"MyGadgets "+Str(AvailPhysMem()/$400)+" KB RAM",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
  AddKeyboardShortcut(0,#PB_Shortcut_F1,1) 
  AddKeyboardShortcut(0,#PB_Shortcut_F2,2)
  AddKeyboardShortcut(0,#PB_Shortcut_Escape,3)
  TextGadget(#PB_Any, 0,400,400,20, "F1: install  -  F2: remove",#PB_Text_Center)
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_Menu
        Select EventMenu()
          Case 1 : CreateGadgets()
          Case 2 : RemoveGadgets()
          Case 3 : Break
        EndSelect
      Case #PB_Event_CloseWindow : Break
    EndSelect
  ForEver
  
  CloseWindow(0)
EndIf

End
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Simple question on FreeGadget()

Post by netmaestro »

Yes. Simple proof:

Code: Select all

OpenWindow(0,0,0,320,240,"")
ContainerGadget(0,0,0,320,240)
ButtonGadget(1,10,10,100,20,"Button")
CloseGadgetList()
Debug IsGadget(1)
FreeGadget(0)
Debug IsGadget(1)
Last edited by netmaestro on Sat Nov 13, 2010 11:45 pm, edited 1 time in total.
BERESHEIT
c4s
Addict
Addict
Posts: 1981
Joined: Thu Nov 01, 2007 5:37 pm
Location: Germany

Re: Simple question on FreeGadget()

Post by c4s »

Did you take a look at the "Library Viewer" -> "Gadget"?
I just did and I think the IDE crashed or something...at least it doesn't respond after closing the test application.
If any of you native English speakers have any suggestions for the above text, please let me know (via PM). Thanks!
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Re: Simple question on FreeGadget()

Post by sverson »

@netmaestro: Thanks - proof positive!
@c4s: no problem here - all removed.

Code: Select all

Procedure.i TestGadgets()
  With MyGadgets
    ResetList(\GadgetList())
    While NextElement(\GadgetList())
      If IsGadget(\GadgetList()\Number)
        ProcedureReturn #False
      EndIf
    Wend
  EndWith
  ProcedureReturn #True
EndProcedure

Procedure RemoveGadgets()
  With MyGadgets
    If ListSize(\GadgetList())
      FirstElement(\GadgetList())
      FreeGadget(\GadgetList()\Number)
      If TestGadgets()
        MessageRequester("Info","All gadgets seem to be removed.")
      Else
        MessageRequester("ERROR","Error on removing gadgets!")
      EndIf
      ClearList(\GadgetList())
    EndIf
  EndWith
  SetWindowTitle(0,"MyGadgets "+Str(AvailPhysMem()/$400)+" KB RAM")
EndProcedure
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Simple question on FreeGadget()

Post by Trond »

Remember that images in imagegadgets and image buttons won't be freed, though.
sverson
Enthusiast
Enthusiast
Posts: 286
Joined: Sun Jul 04, 2004 12:15 pm
Location: Germany

Re: Simple question on FreeGadget()

Post by sverson »

Trond wrote:Remember that images in imagegadgets and image buttons won't be freed, though.
Thanks Trond.
Post Reply