Page 1 of 1

[PB4] Threadsafe - Invalid Memory Access

Posted: Wed Jul 12, 2006 7:21 am
by nco2k
1.) enable threadsafe in compiler options!!

2.) run this "example":

Code: Select all

;DONT FORGET TO ENABLE THREADSAFE IN COMPILER OPTIONS

Procedure Thread() 
  Delay(2000) ;wait two seconds
EndProcedure 

If OpenWindow(0, 0, 0, 100, 100, "WTF?", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateThread(@Thread(), 0) 
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow 
EndIf : End
3.) wait two seconds, then you will get an invalid memory access at EndProcedure

can anyone else confirm this?!

i tried also on a clean install of pb4 with no userlibs etc. but got the same error.

did someone ever tried the threadsafe option?! im a little bit surprised, no one noticed this before. :?

c ya,
nco2k

Posted: Wed Jul 12, 2006 8:22 am
by Flype
you must have ONE (and only one) parameter in your threaded procedure.
this way it works like it should :

Code: Select all

Procedure Thread(parameter.l)
  Delay(2000) ;wait two seconds
EndProcedure

Posted: Wed Jul 12, 2006 10:25 am
by Num3
Strange, i always used parameter free procedures and never had problems!...

But i never used Thread Safe option...

Posted: Wed Jul 12, 2006 11:47 am
by Fred
The parameter is mandatory, even in none threaded mode.

Posted: Wed Jul 12, 2006 5:56 pm
by nco2k
@Num3
me too

@Fred
even so, it shouldnt result in a invalid memory access, no? it works fine in non threadsafe mode. if a parameter is needed that bad, then you should build in a debugger check to see if a parameter is available or not.

c ya,
nco2k

Posted: Wed Jul 12, 2006 6:00 pm
by Fred
It results in an invalid access because there is a missing parameter, that's not a bug. We can't check if the procedure passed has a parameter or not, as it can be anything (a pointer etc.).

Posted: Wed Jul 12, 2006 9:05 pm
by Flype
i already noticed that, and that's exactely the same for all API callbacks.
try to use SetWindowCallback(), CallFastFunction(), or any on a badly defined proc... most of the times it result in an invalid memory access.

Posted: Thu Jul 13, 2006 9:46 pm
by nco2k
@Flype
i know that, but i dont understand why it works without any problems, if you compile it in non threadsafe mode. if the parameter is mandatory, it should result in an invalid memory access even in non threadsafe mode, but it doesnt.

c ya,
nco2k

Posted: Thu Jul 13, 2006 10:41 pm
by freak
Just because under some circumstances it does not crash does not mean you made no mistake.

Posted: Thu Jul 13, 2006 10:59 pm
by netmaestro
I suggest throwing a syntax error if CreateThread is pointed anywhere that isn't a procedure that takes a long parameter, or is not passing a single long parameter. It would clear up any confusion on the subject.

Posted: Thu Jul 13, 2006 11:59 pm
by Flype
yes but how to ?

like fred said, CreateThread() can take a pointer :

Code: Select all

Macro MyDebug(ms, obj)
  Delay(ms)
  Debug obj
EndMacro

Procedure.l myThreadProc(arg.l)
  MyDebug(1000, "thread end")
EndProcedure

*hThreadProc = @myThreadProc()

CreateThread(*hThreadProc, 0)
MyDebug(2000, "main end")

Posted: Fri Jul 14, 2006 1:15 am
by netmaestro
It's just a matter of testing the pointer to see if it points at an existing procedure that takes a long parameter. The addresses of all the procedures are available from the compiler, else the @ function wouldn't be possible.