[fpc-pascal] Re: WSAGetLastError returns 0 when it shouldn't but I cannot see why

Luca Olivetti luca at ventoso.org
Mon Mar 3 10:14:46 CET 2008

En/na Luca Olivetti ha escrit:
> En/na L ha escrit:
>>> Found the problem using my brute force writeln('') skills. It is an 
>>> issue with freepascal's stack or something to do with buggy threads 
>>> methinks. As it is when you call lasterror within another procedure 
>>> with local variables.. they are corrupted or something.
>> Aha.. I changed the by Value parameter to a CONST parameter in the 
>> error procedure (Perror).. And this solves the issue..
>> So something to do with calling by Value within a thread versus a 
>> const (pointer?).
> This seriously worries me, since I use threads extensively, and I mostly 
> use call by value parameters.

I finally had time to test this (do you remember that dilber strip where 
he asks his PHB which one of the 3 projects he's juggling should be 
finished on schedule, and the PHB replies he should finish them all on 
schedule? well, I'm pretty much in the same situation now).
Changing my "procedure PError(msg:string)" to "procedure PError(const 
msg:string)" in TSocketThread.Execute in project2 also fixes the problem.
This smells like a bug somewhere in fpc/rtl, since I don't think it 
should make a difference.
Anyway, this gives me no clue on how to "fix" synapse.
Synapse does this:

function TBlockSocket.SockCheck(SockResult: Integer): Integer;
   if SockResult = integer(SOCKET_ERROR) then
     FLastError := synsock.WSAGetLastError;
     FLastErrorDesc := GetErrorDescEx;
   Result := FLastError;

then in a connect it wraps the connect call in SockCheck:

procedure TBlockSocket.Connect(IP, Port: string);
   Sin: TVarSin;
   SetSin(Sin, IP, Port);
   if FLastError = 0 then
     if FSocket = INVALID_SOCKET then
     SockCheck(synsock.Connect(FSocket, Sin));
     if FLastError = 0 then
     FBuffer := '';
     FLastCR := False;
     FLastLF := False;
   DoStatus(HR_Connect, IP + ':' + Port);


function TBlockSocket.SockCheck(SockResult: Integer): Integer;


function TBlockSocket.SockCheck(const SockResult: Integer): Integer;

does nothing.

Moreover, something really fishy is going on: look at the attached 
project4 (now using synapse).
First I try outside a thread, and it correctly reports error 10060, then 
inside a thread, where it reports 0, then again outside the thread (same 
procedure) and this time it reports 0!

What's going on?


-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: project4.lpr
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20080303/61c86bec/attachment.ksh>

More information about the fpc-pascal mailing list