[PB4] Threadsafe - Invalid Memory Access

Just starting out? Need help? Post your questions and find answers here.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

[PB4] Threadsafe - Invalid Memory Access

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Num3
PureBasic Expert
PureBasic Expert
Posts: 2812
Joined: Fri Apr 25, 2003 4:51 pm
Location: Portugal, Lisbon
Contact:

Post by Num3 »

Strange, i always used parameter free procedures and never had problems!...

But i never used Thread Safe option...
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The parameter is mandatory, even in none threaded mode.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Fred
Administrator
Administrator
Posts: 18252
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post 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.).
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
freak
PureBasic Team
PureBasic Team
Posts: 5944
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Just because under some circumstances it does not crash does not mean you made no mistake.
quidquid Latine dictum sit altum videtur
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post 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")
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

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