Page 1 of 1

Bindevent and Modules

Posted: Fri Jun 02, 2023 8:30 am
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

Re: Bindevent and Modules

Posted: Fri Jun 02, 2023 9:02 am
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.

Re: Bindevent and Modules

Posted: Fri Jun 02, 2023 10:07 am
by Fips
Hi jacdelad,

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

Greetings

Fips