It is currently Mon Sep 23, 2019 8:54 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 11 posts ] 
Author Message
 Post subject: CreateThread: parameter check
PostPosted: Mon Jul 15, 2019 9:52 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
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:
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. :|


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Mon Jul 15, 2019 11:28 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Jul 25, 2005 3:51 pm
Posts: 3573
Location: Utah, USA
@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.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 9:16 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
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:
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:
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:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 9:28 am 
Offline
Enthusiast
Enthusiast
User avatar

Joined: Sun Jun 22, 2003 7:43 pm
Posts: 426
Location: Germany, Saarbrücken
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:
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.

_________________
Electronics, Crazy & Interesting Stuff, all that with text, image and sound? Click here!

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.


Last edited by NicTheQuick on Tue Jul 16, 2019 10:39 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 9:46 am 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
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. ;)


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 10:45 am 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 19, 2005 2:46 pm
Posts: 1792
Location: Pas-de-Calais, France
Interesting... Will check some codes.


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 12:44 pm 
Online
Addict
Addict

Joined: Wed Nov 12, 2008 5:01 pm
Posts: 1053
Location: Russia
Dude wrote:
But what if you accidentally coded it like this:

Code:
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

_________________
Library XP_Menu_Lib - office menu; HID_Lib - USB Library
Torrent client - pbTorrent (source code); Create driver in PureBasic.


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 1:09 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 1:17 pm 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1052
Location: right here
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. :)


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 2:02 pm 
Offline
Addict
Addict
User avatar

Joined: Sat Feb 13, 2010 3:45 pm
Posts: 892
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


Top
 Profile  
Reply with quote  
 Post subject: Re: CreateThread: parameter check
PostPosted: Tue Jul 16, 2019 10:04 pm 
Offline
Addict
Addict

Joined: Mon Feb 16, 2015 2:49 pm
Posts: 1896
#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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye