Page 1 of 1

CreateThread: procedure parameter needed?

Posted: Sun Jul 14, 2019 1:32 pm
by Dude
When creating a thread by calling a procedure, is it mandatory for the procedure name to have a dummy parameter? I just realised my app did NOT have a parameter for a created thread, and so maybe it's why my app is crashing when built as an exe?

But when I tried to crash this test app by NOT having a parameter and building it as an exe, it doesn't crash when run as an exe, so I don't know if the param is needed or not. Could lack of a parameter cause an illegal memory access error though?

Code: Select all

Procedure test() ; No parameter here. Is it needed, though?
  For n=1 To 10
    Debug n
  Next
EndProcedure

OpenWindow(0,200,200,200,100,"test",#PB_Window_SystemMenu)
ButtonGadget(0,20,20,100,25,"test")

Repeat
  Event=WaitWindowEvent()
  If Event=#PB_Event_Gadget
    CreateThread(@test(),0)
  EndIf
Until Event=#PB_Event_CloseWindow

Re: CreateThread: procedure parameter needed?

Posted: Sun Jul 14, 2019 1:35 pm
by NicTheQuick
Yes, it's mandatory. The CreateThread function assumes that the procedure to be started has a parameter and calls it accordingly. If the parameter is missing stack corruption will occur.

Re: CreateThread: procedure parameter needed?

Posted: Sun Jul 14, 2019 1:52 pm
by Dude
NicTheQuick wrote:If the parameter is missing stack corruption will occur.
Excellent news! Now I know why my exe was crashing: I forgot to add a dummy parameter when I changed a procedure from being run from a button, to being run as a thread. Thanks for confirming! :D