Would be useful to add a Destroy Gadget Event so when a window is closed the event can fire
so custom gadget can clean up eg free heap memory.
DestroyGadget Event
DestroyGadget Event
Windows 11, Manjaro, Raspberry Pi OS


Re: DestroyGadget Event
If you have custom gadgetdata stored in a list/map you may use
Or miss I something ?
I wish a command to send an event directly to a gadget...
With PostEvent() I can only send an event to a window...
Code: Select all
Procedure EventCloseWindow()
Protected Window = EventWindow()
If WindowInMyList
; free gadgets memory etc
; maybe a map/list hold the data
; UnBindGadgetEvents.... etc
UnbindEvent(#PB_Event_CloseWindow, @EventCloseWindow(), Window)
EndIf
EndProcedure
I wish a command to send an event directly to a gadget...
With PostEvent() I can only send an event to a window...
Re: DestroyGadget Event
Yes you can do it like that but we shouldn't have to store custom gadgets in a list or map
and should be free to heap allocate them and set a callback to clean them up
and should be free to heap allocate them and set a callback to clean them up
Windows 11, Manjaro, Raspberry Pi OS


Re: DestroyGadget Event
Why can't you just watch for #PB_Event_CloseWindow and then execute your cleanup code?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
"PureBasic won't be object oriented, period" - Fred.
Re: DestroyGadget Event
FreeGadget() does not fire #PB_Event_CloseWindow event. Gadgets are also freed/destroyed automaticallyPB wrote:Why can't you just watch for #PB_Event_CloseWindow and then execute your cleanup code?
when the parent of the gadget (ContainerGadget(), PanelGadget() etc.) is freed, see help for FreeGadget().
So #PB_EventType_Destroy should be send for:
Code: Select all
- FreeGadget() is used with the gadget
- The window that contains the gadget is closed.
- The parent of the gadget (ContainerGadget(), PanelGadget() etc.) is freed.
- The program ends.
Re: DestroyGadget Event
i have a little workaround
Code: Select all
Structure GadgetVT Align 4
GadgetType.l
SizeOf.l
GadgetCallback.i
FreeGadget.i
EndStructure
Structure Gadget
Gadget.i
*vt.GadgetVT
EndStructure
Prototype CallFreeProc(Gadget)
Structure FreeProcInfo
OldProc.CallFreeProc
NewProc.CallFreeProc
GadgedNR.i
EndStructure
Global NewMap OldFreeProc.FreeProcInfo()
;-------------------------------------------
Procedure FreeProc(Gadget)
Protected GadgedNR
If Gadget
GadgedNR = OldFreeProc(Str(gadget))\GadgedNR
OldFreeProc(Str(gadget))\NewProc(GadgedNR)
DeleteMapElement(OldFreeProc(), Str(gadget))
If OldFreeProc(Str(gadget))\OldProc
ProcedureReturn OldFreeProc(Str(gadget))\OldProc(GadgedNR)
EndIf
EndIf
EndProcedure
;-------------------------------------------
Procedure SetFreeProc(Gadget, Proc)
Protected *g.Gadget
*g = IsGadget(gadget)
If *g
OldFreeProc(Str(*g))\GadgedNR = Gadget
OldFreeProc(Str(*g))\OldProc = *g\vt\FreeGadget
OldFreeProc(Str(*g))\NewProc = Proc
*g\vt\FreeGadget = @FreeProc()
EndIf
EndProcedure
;######################################################
Procedure FreeButton(Gadget)
Debug "Free ButtonGadget"
EndProcedure
Procedure FreeString(Gadget)
Debug "Free StringGadget"
EndProcedure
Procedure Main()
Protected event
If OpenWindow(0, #PB_Ignore, #PB_Ignore, 300, 300, "") And InitScintilla()
ButtonGadget(0, 0, 0, 100, 25, "Blub")
ButtonGadget(1, 0, 0, 100, 25, "Blub")
StringGadget(2, 0, 30, 100, 25, "Blub")
ButtonGadget(3, 0, 0, 100, 25, "Blub")
ButtonGadget(4, 0, 0, 100, 25, "Blub")
CanvasGadget(5, 0, 30, 100, 25)
SetFreeProc(0, @FreeButton())
SetFreeProc(1, @FreeButton())
SetFreeProc(2, @FreeString())
SetFreeProc(3, @FreeString())
SetFreeProc(4, @FreeString())
SetFreeProc(5, @FreeButton())
Repeat
event = WaitWindowEvent()
If event = #PB_Event_CloseWindow
Break
EndIf
ForEver
EndIf
EndProcedure: End main()
Last edited by hallodri on Thu Oct 31, 2013 9:52 pm, edited 1 time in total.
Re: DestroyGadget Event
@hallodri
crashes on line 37 with x64 compiler!
crashes on line 37 with x64 compiler!
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
