CreateThread: procedure parameter needed?

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

CreateThread: procedure parameter needed?

Post 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
User avatar
NicTheQuick
Addict
Addict
Posts: 1504
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: CreateThread: procedure parameter needed?

Post 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.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateThread: procedure parameter needed?

Post 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
Post Reply