Bindevent and Modules

Just starting out? Need help? Post your questions and find answers here.
Fips
User
User
Posts: 28
Joined: Sun Feb 20, 2022 1:03 pm

Bindevent and Modules

Post by Fips »

Hi everyone,

I'm having a problem when binding an event inside a module.

First of all I'd like to know if it is in general ok to have several bindEvents for the same event:

Code: Select all

BindEvent(#PB_Event_CloseWindow,@function_1(), 1)
BindEvent(#PB_Event_CloseWindow,@function_2(), 1)
It seems to work, but can this cause problems I'm just not aware of?

If it it ok to do so, please have a look at the following code. There is a function called bindOnCloseEvent which will bind the module-internal function onCloseWindow_3() to the given window. So when the window gets closed all of the bound functions should be called.

If I bind the function via my module-function bindOnCloseEvent after I called the rest of the BindEvent-function, everything seems to be fine.
But if I call it first, my programm will crash and I'll get an 'invalid memory access'-Error on line 31 which is the EndProcedure-Statement of onCloseWindow_1().

Code: Select all

EnableExplicit


DeclareModule Test
  Declare bindOnCloseEvent(win_nr.i)
EndDeclareModule

Module Test
  
  Procedure onCloseWindow_3()
    Debug "onCloseWindow_3 in Module called..."  
  EndProcedure
  
  
  Procedure bindOnCloseEvent(win_nr.i)
    BindEvent(#PB_Event_CloseWindow, @onCloseWindow_3(), win_nr)    
  EndProcedure
  
EndModule


#Win_NR = 1
#Win_NR_Main = 2

Procedure CloseApp()
  End
EndProcedure

Procedure onCloseWindow_1()
  CloseWindow(EventWindow())
EndProcedure


Procedure onCloseWindow_2()
  Debug "onCloseWindow_2 was called"
EndProcedure

UseModule Test

OpenWindow(#Win_NR, 100, 100, 100, 100, "Close Me")

OpenWindow(#Win_NR_Main, 400, 100, 100, 100, "Main")

; PROBLEM IF CALLED HERE!
; bindOnCloseEvent(#Win_NR)

BindEvent(#PB_Event_CloseWindow, @onCloseWindow_1(), #Win_NR)
BindEvent(#PB_Event_CloseWindow, @onCloseWindow_2(), #Win_NR)
BindEvent(#PB_Event_CloseWindow, @CloseApp(), #Win_NR_Main)

; No Problem here
; bindOnCloseEvent(#Win_NR)

Repeat 
  WaitWindowEvent()
  
ForEver

Can anyone tell me what the problem is with binding the event via module-function first?

Greetings

Fips
User avatar
jacdelad
Addict
Addict
Posts: 1478
Joined: Wed Feb 03, 2021 12:46 pm
Location: Planet Riesa
Contact:

Re: Bindevent and Modules

Post by jacdelad »

You can bind an event to multiple functions without problem. The last bound function is called first, then the previous and so on.

In your case the following functions are called (when activating the first function in the module, the "problem"):
onCloseWindow_2()
onCloseWindow_1()
onCloseWindow_3()

So the following happens: onCloseWindow_2() debugs a text, onCloseWindow_1() closes the window and finally onCloseWindow_3() has no windows, that's why it crashes.

Activating the function later, the call is altered (the "no problem"):
onCloseWindow_3()
onCloseWindow_2()
onCloseWindow_1()

Now, first the debug functions debug something and THEN the window is closed.

So keep in mind: When binding -> the last binding is handled first, then the prelast...and lastly the first.
PureBasic 6.04/XProfan X4a/Embarcadero RAD Studio 11/Perl 5.2/Python 3.10
Windows 11/Ryzen 5800X/32GB RAM/Radeon 7770 OC/3TB SSD/11TB HDD
Synology DS1821+/36GB RAM/130TB
Synology DS920+/20GB RAM/54TB
Synology DS916+ii/8GB RAM/12TB
Fips
User
User
Posts: 28
Joined: Sun Feb 20, 2022 1:03 pm

Re: Bindevent and Modules

Post by Fips »

Hi jacdelad,

I was not aware of this.
So no Module issue at all.
Thanks for your explanation.

Greetings

Fips
Post Reply