Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Jonas Maebe

On 14/10/16 02:43, Snorkl e wrote:

Anyway, I am using a third party SFTP server lib and it creates a thread
inherited from tthread and in the create event of the tthread descendant
it does this:
(It uses this thread to read and write data on the socket)

FDataAvailable := TEvent.Create(nil, True, False, '');  //tevent maps to
teventobject

{$IFDEF MSWINDOWS}
  lasterr:=GetLastError;


GetLastError() will return the error code of the last Windows API call 
that has been performed. I doubt TEvent.Create() guarantees not to call 
any Windows API's between creating an event using a Windows event and 
returning. E.g., it might allocate or free memory in between, or the FPC 
RTL may have done that in the context of handling the constructor call.


You should only call GetLastError() immediately after calling a Windows 
API function yourself. In any other case, you cannot know what may have 
transpired in between.



Jonas
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Snorkl e
Hi, actually getlasterror only applies to the calling thread,
getlastOSerror is the one not to use.
Anyway I think I found the solution.
Just for kicks I put in a name with a / I.e /test_name and getlasterror
came back with the same error.  So the API is being called somewhere with
teventobject.create, anyway instead of leaving name blank I put a giud in
for the name and low and behold the error stopped.  It appears there is a
limit or a bug on creating unnamed events from a single process.   When you
leave name blank the os must be creating a unique name somehow and it has
some issue with a lot of them being created and destroyed. When I compiled
as 64bit it took much longer for the error to happen.  After I used the
guid for the event name I stress tested the server with 10s of thousands of
connection and disconnecting and that error never happened again.

So windows limit or bug? Or could it be a bug in  FPC?

On Oct 14, 2016 9:23 AM, "Jonas Maebe"  wrote:

> On 14/10/16 02:43, Snorkl e wrote:
>
>> Anyway, I am using a third party SFTP server lib and it creates a thread
>> inherited from tthread and in the create event of the tthread descendant
>> it does this:
>> (It uses this thread to read and write data on the socket)
>>
>> FDataAvailable := TEvent.Create(nil, True, False, '');  //tevent maps to
>> teventobject
>>
>> {$IFDEF MSWINDOWS}
>>   lasterr:=GetLastError;
>>
>
> GetLastError() will return the error code of the last Windows API call
> that has been performed. I doubt TEvent.Create() guarantees not to call any
> Windows API's between creating an event using a Windows event and
> returning. E.g., it might allocate or free memory in between, or the FPC
> RTL may have done that in the context of handling the constructor call.
>
> You should only call GetLastError() immediately after calling a Windows
> API function yourself. In any other case, you cannot know what may have
> transpired in between.
>
>
> Jonas
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

[fpc-pascal] FPC 3.0.0 and gProf

2016-10-14 Thread Reimar Grabowski
Hi,

are there any known problems with gprof?
Mantis says no but when enabling gprof support in my Lazarus project I get a 
segfault in gmon.c when running it.
Any compiler switches I should check for that may interfere or other hints?

R.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Sven Barth
Am 14.10.2016 17:45 schrieb "Snorkl e" :
>
> Hi, actually getlasterror only applies to the calling thread,
getlastOSerror is the one not to use.

GetLastOSError merely calls GetLastError on Windows...

> Anyway I think I found the solution.
> Just for kicks I put in a name with a / I.e /test_name and getlasterror
came back with the same error.  So the API is being called somewhere with
teventobject.create, anyway instead of leaving name blank I put a giud in
for the name and low and behold the error stopped.  It appears there is a
limit or a bug on creating unnamed events from a single process.   When you
leave name blank the os must be creating a unique name somehow and it has
some issue with a lot of them being created and destroyed. When I compiled
as 64bit it took much longer for the error to happen.  After I used the
guid for the event name I stress tested the server with 10s of thousands of
connection and disconnecting and that error never happened again.
>
> So windows limit or bug? Or could it be a bug in  FPC?

Do you destroy the event again once it's no longer used? Of course if you
require the events the whole time it might indeed be a Windows limitation.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Tomas Hajny
On Fri, October 14, 2016 17:44, Snorkl e wrote:


Hi,

> Hi, actually getlasterror only applies to the calling thread,
> getlastOSerror is the one not to use.
> Anyway I think I found the solution.
 .
 .

I'm glad that you found potential reason of the problem, but your
assumption regarding GetLastError and GetLastOSError is not entirely
correct. You're right that GetLastError returns the last error in the
current thread, because that is written in the respective MSDN
documentation. I don't think that this is in contradiction to the
information from Jonas though.

Moreover, the only difference between GetLastError and GetLastOSError is
the fact that GetLastError is direct part of the MS Windows API (and thus
it behaves exactly as described in MSDN, but it is platform-specific),
whereas GetLastOSError is the FPC RTL abstraction for returning error
information from the underlying operating system (available across
different platforms). As such, you should rely on the behaviour of
GetLastOSError only to the level documented in the FPC documentation (in
other words, certain aspects described in MSDN documentation for
GetLastError may not be valid for GetLastOSError on all FPC supported
platforms due to differences in error handling on different platforms).

As an example, direct calls to Windows API bypassing FPC RTL and resulting
in an error would change the value returned by GetLastOSError on MS
Windows, whereas direct calls to OS/2 API bypassing FPC RTL and resulting
in an error would _not_ change the value returned by GetLastOSError on
OS/2.

The implementation of GetLastOSError for MS Windows (Win32 or Win64)
consists of a direct call to GetLastError and thus the same caveats apply
for them on that platform.

Tomas


>
> On Oct 14, 2016 9:23 AM, "Jonas Maebe"  wrote:
>
>> On 14/10/16 02:43, Snorkl e wrote:
>>
>>> Anyway, I am using a third party SFTP server lib and it creates a
>>> thread
>>> inherited from tthread and in the create event of the tthread
>>> descendant
>>> it does this:
>>> (It uses this thread to read and write data on the socket)
>>>
>>> FDataAvailable := TEvent.Create(nil, True, False, '');  //tevent maps
>>> to
>>> teventobject
>>>
>>> {$IFDEF MSWINDOWS}
>>>   lasterr:=GetLastError;
>>>
>>
>> GetLastError() will return the error code of the last Windows API call
>> that has been performed. I doubt TEvent.Create() guarantees not to call
>> any
>> Windows API's between creating an event using a Windows event and
>> returning. E.g., it might allocate or free memory in between, or the FPC
>> RTL may have done that in the context of handling the constructor call.
>>
>> You should only call GetLastError() immediately after calling a Windows
>> API function yourself. In any other case, you cannot know what may have
>> transpired in between.
>>
>>
>> Jonas


___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Snorkl e
Hi Tomas,

TEventobject.create is the only thing called in the thread constructor, and
teventobject.create is indeed calling a API function in order to create the
event.  There is no other way to find out if teventobject.create failed as
teventobject.handle always has a value even if it fails.
So what I am getting at is its extremely likely getlasterror applies even
though it does not call  winapi directly.

Since I solved it by giving the name param a giud, the next thing to ask is
why does teventobject fail kind of silently when the name parm is blank
over time?

Also keep in mind this is third party code, so I don't really know why they
used teventobject as much as they did, I would imagine they didn't want to
use critical sections all over the place.

And yes the event object does get destroyed when the thread finishes.
I understand what your saying about the differences between getlasterror
and getlastoserror.

I do appreciate the comments :-)

On Oct 14, 2016 12:28 PM, "Tomas Hajny"  wrote:

> On Fri, October 14, 2016 17:44, Snorkl e wrote:
>
>
> Hi,
>
> > Hi, actually getlasterror only applies to the calling thread,
> > getlastOSerror is the one not to use.
> > Anyway I think I found the solution.
>  .
>  .
>
> I'm glad that you found potential reason of the problem, but your
> assumption regarding GetLastError and GetLastOSError is not entirely
> correct. You're right that GetLastError returns the last error in the
> current thread, because that is written in the respective MSDN
> documentation. I don't think that this is in contradiction to the
> information from Jonas though.
>
> Moreover, the only difference between GetLastError and GetLastOSError is
> the fact that GetLastError is direct part of the MS Windows API (and thus
> it behaves exactly as described in MSDN, but it is platform-specific),
> whereas GetLastOSError is the FPC RTL abstraction for returning error
> information from the underlying operating system (available across
> different platforms). As such, you should rely on the behaviour of
> GetLastOSError only to the level documented in the FPC documentation (in
> other words, certain aspects described in MSDN documentation for
> GetLastError may not be valid for GetLastOSError on all FPC supported
> platforms due to differences in error handling on different platforms).
>
> As an example, direct calls to Windows API bypassing FPC RTL and resulting
> in an error would change the value returned by GetLastOSError on MS
> Windows, whereas direct calls to OS/2 API bypassing FPC RTL and resulting
> in an error would _not_ change the value returned by GetLastOSError on
> OS/2.
>
> The implementation of GetLastOSError for MS Windows (Win32 or Win64)
> consists of a direct call to GetLastError and thus the same caveats apply
> for them on that platform.
>
> Tomas
>
>
> >
> > On Oct 14, 2016 9:23 AM, "Jonas Maebe" 
> wrote:
> >
> >> On 14/10/16 02:43, Snorkl e wrote:
> >>
> >>> Anyway, I am using a third party SFTP server lib and it creates a
> >>> thread
> >>> inherited from tthread and in the create event of the tthread
> >>> descendant
> >>> it does this:
> >>> (It uses this thread to read and write data on the socket)
> >>>
> >>> FDataAvailable := TEvent.Create(nil, True, False, '');  //tevent maps
> >>> to
> >>> teventobject
> >>>
> >>> {$IFDEF MSWINDOWS}
> >>>   lasterr:=GetLastError;
> >>>
> >>
> >> GetLastError() will return the error code of the last Windows API call
> >> that has been performed. I doubt TEvent.Create() guarantees not to call
> >> any
> >> Windows API's between creating an event using a Windows event and
> >> returning. E.g., it might allocate or free memory in between, or the FPC
> >> RTL may have done that in the context of handling the constructor call.
> >>
> >> You should only call GetLastError() immediately after calling a Windows
> >> API function yourself. In any other case, you cannot know what may have
> >> transpired in between.
> >>
> >>
> >> Jonas
>
>
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread José Mejuto

El 14/10/2016 a las 20:11, Snorkl e escribió:


Since I solved it by giving the name param a giud, the next thing to ask
is why does teventobject fail kind of silently when the name parm is
blank over time?


Hello,

To me it looks like a memory corruption, in your program or in the 
library. Have you checked the code with heaptrc activated ?



--

___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Snorkl e
Hi Sven,


Yes the events get destroyed in the threads destructor. I verified they
were being freed up.


It would usually happen when lots of connect disconnects happened in a
short time.

Once that error 161 popped it could not be recovered from, all new
connections would fail on the teventobject.create.


It would also happen much sooner when compiled as 32bit.


I guess as long a using the GUID works long term it shouldn't be an issue.



"Do you destroy the event again once it's no longer used? Of course if you
require the events the whole time it might indeed be a Windows limitation.


Regards,

Sven"
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Snorkl e
Yep, I used heaptr, my code is clean.
Sometimes it would happen after 3 connects/disconnects and sometimes after
500.
With the GUID as the event name it has never happened again and I really
stress tested it.

It sounds like windows bug to me, but who knows.

On Oct 14, 2016 1:23 PM, "José Mejuto"  wrote:

> El 14/10/2016 a las 20:11, Snorkl e escribió:
>
> Since I solved it by giving the name param a giud, the next thing to ask
>> is why does teventobject fail kind of silently when the name parm is
>> blank over time?
>>
>
> Hello,
>
> To me it looks like a memory corruption, in your program or in the
> library. Have you checked the code with heaptrc activated ?
>
>
> --
>
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Sven Barth
Am 14.10.2016 20:31 schrieb "Snorkl e" :
>
> Yep, I used heaptr, my code is clean.
> Sometimes it would happen after 3 connects/disconnects and sometimes
after 500.
> With the GUID as the event name it has never happened again and I really
stress tested it.
>
> It sounds like windows bug to me, but who knows.

Looking at the code of the RTL it could be a FPC bug. FPC calls
CreateEvent() basically like this:

Result := CreateEvent(..., PChar(Name));

Now the point is that by Name being a String the result of PChar(Name) is
always a valid string consisting only of #0. That however is not equal to
Nil which the documentation on MSDN states as necessary for an anonymous
event.

Are you able to compile FPC and its RTL yourself? In that case I'd like to
ask you to adjust the function BasicEventCreate in
$fpcdir/rtl/win/systhrd.inc like this:

var
  n: PChar;
begin
  new(plocaleventrec(result));
  if Length(Name) = 0 then
n := Nil
  else
n := PChar(Name);
  plocaleventrec(result)^.FHandle := CreateEvent(EventAttributes,
AManualReset, InitialState, n);
end;

With this and rebuilt RTL, packages and Co I'd ask you to test your code
again and report back whether that solves the problem.

Regards,
Sven
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Re: [fpc-pascal] teventobject.create fails with error 161

2016-10-14 Thread Snorkl e
Hi Sven,
Yes I can try that this weekend sometime and will respond with results,.
Thanks for taking a look at that 😁

On Oct 14, 2016 3:41 PM, "Sven Barth"  wrote:

> Am 14.10.2016 20:31 schrieb "Snorkl e" :
> >
> > Yep, I used heaptr, my code is clean.
> > Sometimes it would happen after 3 connects/disconnects and sometimes
> after 500.
> > With the GUID as the event name it has never happened again and I really
> stress tested it.
> >
> > It sounds like windows bug to me, but who knows.
>
> Looking at the code of the RTL it could be a FPC bug. FPC calls
> CreateEvent() basically like this:
>
> Result := CreateEvent(..., PChar(Name));
>
> Now the point is that by Name being a String the result of PChar(Name) is
> always a valid string consisting only of #0. That however is not equal to
> Nil which the documentation on MSDN states as necessary for an anonymous
> event.
>
> Are you able to compile FPC and its RTL yourself? In that case I'd like to
> ask you to adjust the function BasicEventCreate in
> $fpcdir/rtl/win/systhrd.inc like this:
>
> var
>   n: PChar;
> begin
>   new(plocaleventrec(result));
>   if Length(Name) = 0 then
> n := Nil
>   else
> n := PChar(Name);
>   plocaleventrec(result)^.FHandle := CreateEvent(EventAttributes,
> AManualReset, InitialState, n);
> end;
>
> With this and rebuilt RTL, packages and Co I'd ask you to test your code
> again and report back whether that solves the problem.
>
> Regards,
> Sven
>
> ___
> fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
> http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
>
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal