CreateThread: parameter check

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

CreateThread: parameter check

Post by Dude »

Hi Fred, since PureBasic knows each procedure definition while compiling, I would request that when CreateThread() is called for a procedure, that if the procedure has NO parameter then an error be raised. Based on this post: viewtopic.php?f=13&t=73189

I literally just spent weeks trying to find why my app kept crashing with an illegal memory access error, and it was because my procedure name was simply Proc() instead of Proc(p) when creating the thread. Yes, 1 single missing byte in my 900,000+ byte source was bringing my app to its knees. A true needle in the haystack! :lol:

So, a warning by the compiler that Proc() had no parameter would've alerted me to the problem instantly. Thank you.

And to stave off any forthcoming complaints that the compiler shouldn't check all procedure parameters: CreateThread() is an exception because by its very definition it gets you to name the procedure without any parameters in its name:

Code: Select all

Thread = CreateThread(@ProcedureName(), *Value)
So it's easy to see from this Help manual example that it looks like no parameters are needed, even though they are. :|
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CreateThread: parameter check

Post by Demivec »

@Dude: Just a comment on your request.

I think it is a good idea and I am very much in favor of your request. I would raise the concern though that there are also other ways the parameter value might be specified besides the one you mentioned.

The 'address' of a procedure is what is supplied as one of the parameters to CreateThread() and not the name of a procedure. I bring this up because I may use this instead 'CreateThread(*procedure(x), *value)' and the feature you requested still wouldn't help.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateThread: parameter check

Post by Dude »

Demivec wrote:I may use this instead 'CreateThread(*procedure(x), *value)' and the feature you requested still wouldn't help.
Hi Demivec, I think you misunderstood what I mean.

When creating a thread, the procedure being called must be defined to accept one parameter. An example direct from the manual:

Code: Select all

Procedure YourProcedure(*Value) ; *Value = The required parameter.
  ; The variable '*Value' will contain 23
EndProcedure

CreateThread(@YourProcedure(), 23)
But what if you accidentally coded it like this:

Code: Select all

Procedure YourProcedure() ; We forgot to put *Value here this time. Oops!
  ; The variable '*Value' won't contain 23 because *Value doesn't exist.
EndProcedure

CreateThread(@YourProcedure(), 23)
This causes stack corruption (and your app to crash) because no parameter is being accepted by the Procedure definition line. So my wish is that when CreateThread() is used, the IDE will recognize that YourProcedure() didn't have a parameter (due to opening/closed brackets), and will raise an error/warning for you.

Or, to take your example: you say you use "CreateThread(*procedure(x), *value)". But what happens if your procedure was defined like this:

Code: Select all

Procedure Demivec()
EndProcedure
Where is your "x" value going to go? Nowhere, so when the compiler sees "CreateThread(*procedure(x), *value)" it should warn that the procedure for "Demivec()" had no parameters.
User avatar
NicTheQuick
Addict
Addict
Posts: 1224
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: CreateThread: parameter check

Post by NicTheQuick »

What Demivec wanted to say was that you can give CreateThread() only a pointer to a procedure and then the compiler does not know which procedure you're actually calling:

Code: Select all

Procedure mythread(dummy.i)
EndProcedure

Define *somePointer = @mythread()

CreateThread(*somePointer, 0)
Of course that pointer could be saved anywhere deep in some structures or linked lists. The compiler will not be able to tell if you call CreateThread with the right procedure at compile time.

Edit: Corrected spelling.
Last edited by NicTheQuick on Tue Jul 16, 2019 10:39 am, edited 1 time in total.
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: parameter check

Post by Dude »

NicTheQuick wrote:What Demivec wanted to say was that you can only need to give CreateThread() a pointer to a procedure and then the compiler does not know which procedure you're actually calling
Oh, I didn't realize that. Now I see. But still, my request stands for when a procedure name is directly mentioned in CreateThread() (as its literal text name and not as a pointer; in your case: "mythread" instead of "*somePointer") and that can be matched up with an existing Procedure definition. ;)
User avatar
djes
Addict
Addict
Posts: 1806
Joined: Sat Feb 19, 2005 2:46 pm
Location: Pas-de-Calais, France

Re: CreateThread: parameter check

Post by djes »

Interesting... Will check some codes.
User_Russian
Addict
Addict
Posts: 1443
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CreateThread: parameter check

Post by User_Russian »

Dude wrote:But what if you accidentally coded it like this:

Code: Select all

Procedure YourProcedure() ; We forgot to put *Value here this time. Oops!
  ; The variable '*Value' won't contain 23 because *Value doesn't exist.
EndProcedure

CreateThread(@YourProcedure(), 23)
This causes stack corruption (and your app to crash) because no parameter is being accepted by the Procedure definition line. So my wish is that when CreateThread() is used, the IDE will recognize that YourProcedure() didn't have a parameter (due to opening/closed brackets), and will raise an error/warning for you.
PB reported an error.

Image
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateThread: parameter check

Post by Dude »

User_Russian wrote:PB reported an error.
Yes, in such a small non-real-world example. But what about when you compile your exe and don't create the thread until later, like here: viewtopic.php?f=13&t=73189

Then the error isn't apparent until the user clicks the button, and thus runs the risk of a crash. That's what was happening to my users. The debugger doesn't (currently) report an error for that type of situation.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: CreateThread: parameter check

Post by #NULL »

Dude wrote:
User_Russian wrote:PB reported an error.
Yes, in such a small non-real-world example. But what about when you compile your exe and don't create the thread until later, like here: viewtopic.php?f=13&t=73189

Then the error isn't apparent until the user clicks the button, and thus runs the risk of a crash. That's what was happening to my users. The debugger doesn't (currently) report an error for that type of situation.
Then you have untested branches in your code, which is bad. :)
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: CreateThread: parameter check

Post by Josh »

Dude wrote:Yes, in such a small non-real-world example. But what about when you compile your exe and don't create the thread until later, like here: viewtopic.php?f=13&t=73189
Then the error isn't apparent until the user clicks the button, and thus runs the risk of a crash. That's what was happening to my users. The debugger doesn't (currently) report an error for that type of situation.
I think this situation is far-fetched. In my small-real-world of programming, I am sure to run on the CreateThread() error message with the debugger.

I have to agree with the previous speakers that it is not guaranteed that the @-operator is used and the compiler has direct access to the procedure. Especially with threads one should assume that the programmer knows what he is doing.
sorry for my bad english
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CreateThread: parameter check

Post by Dude »

#NULL wrote:you have untested branches in your code, which is bad
Originally it wasn't untested, because my procedure wasn't a thread (it was triggered by a button, as shown in my other example that I linked to). But then I made it a thread for a specific reason but simply forgot that the procedure now needed a parameter - a very easy thing to overlook when you haven't used threads much. Then I added new features and updated some other areas of code, and noticed my app was crashing, so I spent weeks going over the new and changed stuff, thinking they were the cause, when in reality is was just one single missing little letter "p" for the procedure parameter.

So it wasn't fun and of course totally my fault, but that's the reason for this request: so that a forgotten parameter could be alerted to the coder by the compiler if CreateThread is called to it. And that request, following your logic, would lead to tested branches in future. ;)

Anyway, the request is out there, and I hope it can be done. That's all I can do, even if others think it's a dumb idea.
Post Reply