Problem with GetLastError()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Psych
Enthusiast
Enthusiast
Posts: 239
Joined: Thu Dec 18, 2008 3:35 pm
Location: Wales, UK

Problem with GetLastError()

Post by Psych »

Seems like procedures which use debug statements clear the getlasterror value.
Also, any procedure which returns a string clears the value, even if the procedure is empty.
For debugging this can be critical.
Comment out the 'debug' inside the procedure and the call to the string function and see the error data is not affected.

Code: Select all

SetLastError_(2)
Procedure do_anything()
	Debug "proc"
EndProcedure
Procedure$ string_function()
	
EndProcedure
do_anything()
string_function()
Debug "main"
Debug GetLastError_()
Might I suggest safeguarding system error information in future releases as to preserve debugging information.
----------------------------------------------------------------------------
Commenting your own code is admitting you don't understand it.
----------------------------------------------------------------------------
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: Problem with GetLastError()

Post by GPI »

GetLastError_() is a windows-API-Function not a PB-Library-Function. You should only use this function after Windows-Api-Calls.
Psych
Enthusiast
Enthusiast
Posts: 239
Joined: Thu Dec 18, 2008 3:35 pm
Location: Wales, UK

Re: Problem with GetLastError()

Post by Psych »

I could not agree more, I am quite aware what GetLastError_() does.
That is my point.
Since we are in agreement that it's a windows API function and not a PB-Library function, why does PB clear/modify the GetLastError_() result?
The internal windows error code should only be modified by another API function (where applicable).
If a developer wishes to use to Windows API, it's use should not be sabotaged by PB behind the scenes.
----------------------------------------------------------------------------
Commenting your own code is admitting you don't understand it.
----------------------------------------------------------------------------
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Problem with GetLastError()

Post by Danilo »

PB functions use API internally, as well. For example, the Debug statement adds text to the debug output window, using an API call.
GetLastError_() is only valid directly after an API call.
Psych
Enthusiast
Enthusiast
Posts: 239
Joined: Thu Dec 18, 2008 3:35 pm
Location: Wales, UK

Re: Problem with GetLastError()

Post by Psych »

Danilo wrote:the Debug statement adds text to the debug output window, using an API call.
Interesting theory, but add comments to BOTH function calls.
Output will be 2.
Despite debugging the word 'main' to the debugger window.

In addition, calling the empty function that returns a string also invalidates API error values, despite the function doing nothing.

Maybe this is just a bug in the way functions are called/handled.
----------------------------------------------------------------------------
Commenting your own code is admitting you don't understand it.
----------------------------------------------------------------------------
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Problem with GetLastError()

Post by Danilo »

Psych wrote:Interesting theory, but add comments to BOTH function calls.
I think it's even more complicated. Even internal string management functions could call API
for allocating and de-allocating memory/heap, copy memory/strings, etc.

Anyway, it's your feature request and I don't want to speak against it.

I just think for Set/GetLastError_() to work only with the WinAPI you use yourself in your PB code,
every single PB function (and internals like string management, debugger, etc.) that also use WinAPI
must preserve the WinAPI LastError state. This would/could probably affect nearly all functions.
Post Reply