[fpc-pascal] Re: I need an lNet expert: multiple connections and only one central CallAction() - is this possible?

2012-05-29 Thread Bernd Kreuss
On 28.05.2012 16:02, Bernd wrote:

 What I would have expected would be some API where I can register
 all these connections (outgoing connections, listening sockets,
 etc) and then have only one thread blocking in only one call that
 will watch all these objects at once, but I can't find any
 explanation how this is supposed to be done with lNet.

I can now answer my own question:

It seems it is meant to be used in the following way. The first thing
I have to do is to have my own instance of a TLEventer class:

  FEventer: TLEventer;

and instantiate it:

  FEventer := BestEventerClass.Create;

and then set a short timeout (1000ms or so) to make the eventer
blocking but still be able to check the termination of the thread in
regular intervals and create a thread that calls CallAction in an
infinite loop:

  procedure TEventerThread.Execute;
  begin
FEventer.TimeOut := 1000;
repeat
  FEventer.CallAction;
until Terminated;
  end;

And then I can create as many TLTcp instances as I need and for every
such TLTcp the first thing after creation is to set the Eventer property:

  FLnetListener := TLTcp.Create(self);
  with FLnetListener do begin
Eventer := FEventer;
OnAccept := @OnListenerAccept;
FLnetListener.Listen(FListenPort);
  end;

and even more such objects for outgoing connections (one for each
connection), each setting the Eventer to the same one and only eventer
instance:

constructor TBuddy.Create(AClient: IClient);
  inherited Create;
  FClient := AClient;
  FLnetClient := TLTcp.Create(nil);
  FLNetClient.Eventer := AClient.Eventer;
  ...
end;

procedure TBuddy.InitiateConnect;
begin
  with FLnetClient do begin
OnConnect := @Self.OnProxyConnect;
OnReceive := @Self.OnProxyReceive;
OnDisconnect := @Self.OnProxyDisconect;
OnError := @Self.OnProxyError;
  end;
  FLnetClient.Connect(FClient.TorHost, FClient.TorPort);
end;


Seems to work quite well, I only have to find out how to properly shut
down incoming connections that were disconnected by the other side and
then start rapidly firing recv events with 0 bytes (CPU goes 100%),
the only way seems to be to set the Dispose property of the TLHandle
to true once I receive 0 bytes for the first time on this connection
and then pretend the connection is closed but I am not sure if this is
allowed and I am not leaking OS handles that way. Also with my code I
seem to be able to produce strange crashes inside heaptrc that I have
never seen before, still investigating how to reproduce.

Bernd

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


Re: [fpc-pascal] Debug information

2012-05-29 Thread Bernd Kreuss
On 29.05.2012 10:25, Rainer Stratmann wrote:
 I will try later.

Also make sure you have turned *off* all optimizations, they can
sometimes mess up the stack trace badly (by removing unneeded stack
frames which can produce very confusing results) and also I have
witnessed at least one occasion where smart linking also somehow
interfered with debugging (but this might have been a gdb problem),
but maybe better turn this off also, just to be on the safe side.

Building your own compiler and RTL from source with debugging symbols
only sounds complicated the very first time you hear about this idea,
once you have successfully done it yourself you will find that it is
actually quite easy and not more complicated than installing a
compiler via the .deb packages.

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


Re: [fpc-pascal] FPC 2.4.2 source

2011-05-21 Thread Bernd Kreuss
On 21.05.2011 11:29, Mattias Gaertner wrote:
 On Sat, 21 May 2011 12:12:59 +0300
 Juha (gmail) juha.mannine...@gmail.com wrote:
 
 [...]
 When trying to update the new lazarus and fpc binaries with deb packages I
 got in trouble with dependancies. Then I abort it.
 
 What troubles?

The thing I have never understood is why it split into a myriad of
different .deb files while it really only needs to be the following:

* lazarus.deb
* fpc.deb
* fpc-source.deb

and maybe even alternatively for lazy people like me something like

* lazarus-complete.deb (provides: lazarus, fpc, fpc-source)

I'm not sure about the exact entries in the deb control files (provides,
depends, replaces, etc.) to achieve this magic but I think it should be
possible, shouldn't it?

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


Re: [fpc-pascal] FPC 2.4.2 source

2011-05-21 Thread Bernd Kreuss
On 21.05.2011 12:11, Mattias Gaertner wrote:
 The thing I have never understood is why it split into a myriad of
 different .deb files
 
 Because of Debian policies.

There exists a debian policy to split a large package into some randomly
chosen smaller ones although *none* of them can ever be used
individually without *all* others?

I didn't know this. The only policy I know of is that there is a
convention to separate packages for binary and source (and headers if
C/C++) and even is not generally applicable to all programming languages.

I would just make one binary package and one source package and ignore
the affectivities of the debian bureaucrats and let them split it on
their own if they so desperately want to split things for no reason that
have always belonged together. I promise: They wont. They will take the
package as it is. Don't ever let your productivity be hindered by some
crazy Debian bureaucrats, don't let them dictate you how to architect
your own software, don't waste any valuable time trying to fulfill their
crazy unreasonable wishes. Just ignore them. Don't listen to them when
they are trying to educate you about your own software, they have no
clue about it. Do what makes sense and ignore everything that has no
other reason than pure bureaucracy. They will bow (rather sooner than
later) because they simply have no other choice and either do the work
themselves or (more likely) simply take the package more or less as it is.

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


Re: [fpc-pascal] FPC 2.4.2 source

2011-05-21 Thread Bernd Kreuss
On 21.05.2011 12:21, Mattias Gaertner wrote:
 A README.txt would
 help. For example:
 If you want to install the gtk2 version of Lazarus use:

A shell script that installs them in the correct order.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] FPC 2.4.2 source

2011-05-21 Thread Bernd Kreuss
On 21.05.2011 12:59, Jonas Maebe wrote:

 They don't want to force everyone to install firebird, mysql, postgresql, 
 ptcgraph, gtk, gtk2, qt and every other single thing for which FPC has 
 bindings.

force is the keyword here. The entire following rant refers to only
this one word:

Its not their [Debian] business to find non-solutions for these
non-problems, I would just ignore this.

They are first making up a non-existing problem (people being forced
to do something they allegedly don't want, although we all know the
opposite is true and people *always* want a complete and painless
install to avoid all sorts of problems) and then they are presenting the
non-solution to this non-problem: Do a lot of work to split things that
always belonged together while creating a lot of new problems and
dependency hell nobody has asked for and nobody ever wanted.

Just ignore what they [Debian] want. It is not relevant what they
believe (or guess) what your users might want. Don't let them damage the
usability and the reputation by listening to them and wasting time
solving their fantasy problems while this is leading to the creation new
and very real problems. The sheer existence of this thread (and other
threads like this and many more) is testifying this.

The Debian maintainers have no expertise in judging what the users of a
given software really *want*, especially if they don't use it
themselves. Simply ignore them. Their opinion does not matter. They will
accept the big package. Don't bow to them, they want something from you,
not the other way around!

The windows installer of Lazarus clearly shows how usability looks like.
This is what users want! There is no reason why this should not be
possible on Debian also. If Debian policies (or like in this case not
even the written policies themselves but only the beliefs of some
overzealous Debian maintainers) are the only thing preventing usability
from coming true then to hell with them.

Bernd

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


Re: [fpc-pascal] Name of the programming language used in/with FPC

2011-02-19 Thread Bernd Kreuss
On 08.02.2011 09:34, Ben wrote:

 What is the programming language used in and with the Free Pascal Compiler?
 
 * Object Pascal
 * Delphi
 * Free Pascal
 * Pascal

How about calling it Pascal, implicitly stating that Pascal nowadays
is OO anyways, a fact that does not need to be mentioned separately, and
also implicitly declaring the implementation of Free Pascal with all its
features as the legitimate heir of any older Pascal and today's de-facto
standard?

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


[fpc-pascal] negative numbers in CurrHeapUsed and MaxHeapUsed

2010-10-22 Thread Bernd Kreuss
Does anybody know what has become of this:
http://free-pascal-general.1045716.n5.nabble.com/TFPCHeapStatus-miscalculations-in-2-3-1-td2824362.html

I am currently experiencing the same problem. The only thing my Program
does on the heap are ansistring manipulations, concatenating strings to
new strings and discarding intermediate results. The total amount used
by my strings should not grow they should be all discarded after
processing, they are all only temporarily used in local variables inside
procedures.

CurrHeapUsed starts around 4000 and then after a few hours slowly
shrinks(!) until it eventually becomes negative (not always, there is
some randomness in my data and the amount of strings that are processed,
sometimes I have to wait longer). MaxHeapUsed starts with 16576 and then
suddenly drops to -16.

This all happens with FPC 2.5.1, snapshot from a few days ago.

Is this a known problem and nothing to worry about or am I asking for
trouble if I ignore this?

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


Re: [fpc-pascal] negative numbers in CurrHeapUsed and MaxHeapUsed

2010-10-22 Thread Bernd Kreuss
On 22.10.2010 21:23, Jonas Maebe wrote:

 It's only a cosmetic problem, there is no problem with the heap itself. It's 
 a known bug, related to reallocmem: 
 http://bugs.freepascal.org/view.php?id=15805

If I understand this correctly then these are only meaningless counters
that come out of sync. Is there a way to ask the heap manager directly
for the real numbers? The heap manager must somehow be able to know
this or calculate this, it cannot rely on these broken counters or it
would eventually crash or otherwise misbehave, wouldn't it?

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


Re: [fpc-pascal] Re: Can variables be declared within a block?

2010-10-18 Thread Bernd Kreuss
On 18.10.2010 15:19, Lukasz Sokol wrote:

 Having variables declared within code block used to require to parse the 
 source code
 at least 2 times

Or create them on the stack at the begin and remove them at the end of
the block (which would be the only way to have some additional
functionality through this).

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


Re: [fpc-pascal] Can variables be declared within a block?

2010-10-18 Thread Bernd Kreuss
On 18.10.2010 15:55, Frank Church wrote:

 I just want to create shorter variables like
 si :=  StringList.NameOfIndex(TableName.FieldByName('').AsString);
 ae := xxx;
 so:= yyy
 
 to express complicated logic with them.

Maybe the usage of WITH could be helpful in some of these cases

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


Re: [fpc-pascal] Can variables be declared within a block?

2010-10-18 Thread Bernd Kreuss
On 18.10.2010 15:55, Frank Church wrote:

 I understand the need to avoid stuff that could create unintended errors

It would create something that I would call irregularity (or even
ugliness) in the language grammar. Nowhere else is it ever allowed to
have certain things inside a begin/end block, type, const, var are all
only allowed in the *same* certain places outside of such blocks.
Allowing var would also raise the question why not also allow type and
const and this would open a can of worms. It would destroy some (much)
of the simplicity (regularity, strictness, beauty, I don't know a better
word) of the grammar.

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


Re: [fpc-pascal] fpc.cfg on windows

2010-10-10 Thread Bernd Kreuss
On 10.10.2010 13:54, Sven Barth wrote:

 In trunk version of Lazarus you can set a configuration as default for 
 new projects.

[OT] If the same option that can put an @extracfg.cfg on a per project
basis would also be implemented in Lazarus' environment options to put
another @globalextracfg.cfg behind it for *all* projects, this would be
nice and provide enough flexibility.

[On-Topic again] Can multiple @someconfig.cfg options be chained on the
command line and it will then include them all in this order (I admit I
am too lazy to try this now but I suspect this is possible)?

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


Re: [fpc-pascal] fpc.cfg on windows

2010-10-10 Thread Bernd Kreuss
On 10.10.2010 15:30, Bernd Kreuss wrote:

 to put
 another @globalextracfg.cfg behind it 

I wanted to say in front of it.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Disk Full

2010-10-10 Thread Bernd Kreuss
The following code (Msg is just outputting to the debug monitor) will
produce something strange:

// replace all line endings with space
for Pos1 := 1 to Len do begin
  try
Msg(2, 'GetVector', Format('  %d %d',[Pos1, Len]));
if Line[Pos1] = #13 then Line[Pos1] := ' ';
if Line[Pos1] = #10 then Line[Pos1] := ' ';
  except
on e: Exception do
  Msg(2, 'GetVector', Format('e %d %d %s',[Pos1, Len, e.message]));
  end;
end;

 [1428] 2 TRConsole.GetVector:   1 84
 [1428] 2 TRConsole.GetVector:   2 84
 [1428] 2 TRConsole.GetVector:   3 84
 [1428] 2 TRConsole.GetVector: e 3 84 Disk Full
 [1428] 2 TRConsole.GetVector:   4 84
 [1428] 2 TRConsole.GetVector:   5 84
 [1428] 2 TRConsole.GetVector:   6 84
 [1428] 2 TRConsole.GetVector:   7 84
 [1428] 2 TRConsole.GetVector:   8 84
 [1428] 2 TRConsole.GetVector:   9 84
 [1428] 2 TRConsole.GetVector: e 9 84 Disk Full
 [1428] 2 TRConsole.GetVector:   10 84
 [1428] 2 TRConsole.GetVector:   11 84
 [1428] 2 TRConsole.GetVector:   12 84
 [1428] 2 TRConsole.GetVector:   13 84
 [1428] 2 TRConsole.GetVector:   14 84
 [1428] 2 TRConsole.GetVector:   15 84
 [1428] 2 TRConsole.GetVector: e 15 84 Disk Full
 [1428] 2 TRConsole.GetVector:   16 84
 [1428] 2 TRConsole.GetVector:   17 84
 [1428] 2 TRConsole.GetVector:   18 84
 [1428] 2 TRConsole.GetVector:   19 84
 [1428] 2 TRConsole.GetVector:   20 84
and so on.

This is not the only code that fails in strange ways! I wondered all the
time why I got random non-deterministic crashes all the time and only
when I inserted these debug message outputs into this loop the nature of
the problem became more clear.

The code in question is running in a dll that is loaded by a the
proprietary software Metatrader 4 (through their proprietary scripting
language) and it does not much more than having a TProcess in which
Rterm.exe (the R-Project) is running (and idle) and there are two pipes
for the stdio of this process but they are empty and idle when this
happens. I have no other threads started when this happens (of course
Metatrader has a few) but not my dll and nothing calls into my dll when
this happens.

FPC is 2.5.1 trunk.

There are no OS exceptions happening because I have another dll with an
exception handler hooked into AddVectoredExceptionHandler() as the very
forst handler that does reliable log all these windows exceptions (AV,
FPU, etc).

What could cause this strange effect? What kind of outside force can
make this thread raise an FPC native exception although it does not come
anywhere near a raise statement? Maybe there is something wrong in the
DLLMain when the attach_thread event happens? does it mess up this
thread ID (or however the raise mechanism works) with something else?

Maybe something else is permanently calling raise and random other
threads are catching it?

confused,
Bernd
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Re: Disk Full

2010-10-10 Thread Bernd Kreuss
On 10.10.2010 23:42, Bernd Kreuss wrote:
 [1428] 2 TRConsole.GetVector:   1 84
 [1428] 2 TRConsole.GetVector:   2 84
 [1428] 2 TRConsole.GetVector:   3 84
 [1428] 2 TRConsole.GetVector: e 3 84 Disk Full
 [1428] 2 TRConsole.GetVector:   4 84
 [1428] 2 TRConsole.GetVector:   5 84
 [1428] 2 TRConsole.GetVector:   6 84
 [1428] 2 TRConsole.GetVector:   7 84
 [1428] 2 TRConsole.GetVector:   8 84
 [1428] 2 TRConsole.GetVector:   9 84
 [1428] 2 TRConsole.GetVector: e 9 84 Disk Full

What do you suggest I could try to narrow down the reason for this?

PS: Is there any possibility to make a DLL that has a completely empty
DLLMain or one that I can entirely write myself without having to patch
the RTL?
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Re: Disk Full

2010-10-10 Thread Bernd Kreuss
from rtl/win/syswin.inc:

DLL_THREAD_ATTACH :
  begin
inclocked(Thread_count);
 
WinEnterCriticalSection(AttachingThread);
if Win32GetCurrentThreadId  MainThreadIdWin32 then
begin
  { Allocate Threadvars  }
  SysAllocateThreadVars;
 
  { NS : no idea what is correct to pass here - pass dummy value 
 for now }
  { passing a dummy is ok, the correct value is read from the coff 
 header of SysInstance (FK) }
  InitThread($100); { Assume everything is idempotent there, 
 as the thread could have been created with BeginThread... }
end;
 
if assigned(Dll_Thread_Attach_Hook) then
  Dll_Thread_Attach_Hook(DllParam);
Dll_entry:=true; { return value is ignored }
WinLeaveCriticalSection(AttachingThread);
 end;

will this also work when some object in my own dll is starting a thread
with TThread that is already properly initialized or will it then try to
initialize it twice?
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Re: Disk Full

2010-10-10 Thread Bernd Kreuss
On 10.10.2010 23:42, Bernd Kreuss wrote:
 Disk full

Please ignore this thread and don't waste any more time thinking about it.

It was my own stupidity. My über-cool debug log function had a stupid
writeln in it that should not have been used when running on windows and
no stdout is available. This writeln did cause the disk full.

All problems seem to be gone now.

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


[fpc-pascal] fpc.cfg on windows

2010-10-09 Thread Bernd Kreuss
Hi,

I cannot find any documentation on this: On Linux I can have my global
fpc.config in /etc/fpc.cfg and it will also look for a config in
~/.fpc/fpc.cfg which is very convenient since I can put some unit search
paths there that I don't want to specify in the Lazarus project file
itself because the paths are on network drives and mounted in different
locations on the different machines that I use to compile the same projects.

On Windows it seems the global configuration is in the bin folder since
windows does not have an equivalent of /etc but where is the per-user
configuration? I have tried %appdata%/fpc/fpc.cfg and %appdata%/fpc.cfg
and %appdata%/.fpc/fpc.cfg and a few more combinations with and without
dots but none of them seems to work. Where does fpc look for this file?

confused,
Bernd
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Re: TProcess.Free - three exceptions in ntdll.dll - only *sometimes*

2010-10-05 Thread Bernd Kreuss
On 04.10.2010 19:23, Bernd Kreuss wrote:
 Hi,
 
 has anybody ever had the problem of TProcess.Free causing
 three exceptions in in ntdll.dll?
 
 C005 at 7C928FEA in C:\WINDOWS\system32\ntdll.dll

Just for the record: It was not TProcess and also not the streams, at
least not directly: After further experimenting and simplifying the dll
until it finally consisted of nothing more than only two functions that
would create and destroy some dummy object without any functionality I
still saw this exception immediately when freeing the object and the
same exception again *after* the host app had unloaded my dll.

I have now put the cmem unit into my uses (I always only saw this
mentioned in combination with the cthreads for *ix, never thought that
it was usable on win32 too) and the exceptions while freeing obects seem
to have all gone away. However I still see exactly one such exception
*after* unloading the dll but the host app seems to be able to catch it
without crashing.

(the host app is unfortunately a commercial closed source application
and probably written in C++ [at least it is definitely not from any
borland or compatible compiler since FPU exceptions are masked])

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


[fpc-pascal] Re: TProcess.Free - three exceptions in ntdll.dll - only *sometimes*

2010-10-05 Thread Bernd Kreuss
On 04.10.2010 19:23, Bernd Kreuss wrote:

 C005 at 7C928FEA in C:\WINDOWS\system32\ntdll.dll
 
 7C928FEA seems to be RtlpWaitForCriticalSection() somewhere deep inside
 the windows kernel.

I see this error message now exactly once, immediately *after* the DLL
has been unloaded by the host application.

I'm just looking at the code below (from fpc 2.5.1 trunk,
rtl/win/syswin.inc) and I wonder whether this usage of the critical
section functions might eventually be related to this? (I am still
trying to completely understand this and try to understand why it is
allowed or needed to put these init/done calls in two separate calls of
dllmain, especially this sudden exit in DLL_PROCESS_DETACH caught my eye:


function Dll_entry{$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}(const info
: TEntryInformation){$endif FPC_HAS_INDIRECT_MAIN_INFORMATION} :
longbool; [public,alias:'_FPC_DLL_Entry'];
  begin
{$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
 EntryInformation:=info;e
{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
 IsLibrary:=true;
 Dll_entry:=false;
 case DLLreason of
   DLL_PROCESS_ATTACH :
 begin
   WinInitCriticalSection(AttachingThread);
   MainThreadIdWin32 := Win32GetCurrentThreadId;

   If SetJmp(DLLBuf) = 0 then
 begin
{$ifdef FPC_HAS_INDIRECT_MAIN_INFORMATION}
   EntryInformation.PascalMain();
{$else FPC_HAS_INDIRECT_MAIN_INFORMATION}
   PascalMain;
{$endif FPC_HAS_INDIRECT_MAIN_INFORMATION}
   Dll_entry:=true;
 end
   else
 Dll_entry:=DLLExitOK;
 end;
   DLL_THREAD_ATTACH :
 begin
   inclocked(Thread_count);

   WinEnterCriticalSection(AttachingThread);
   if Win32GetCurrentThreadId  MainThreadIdWin32 then
   begin
 { Allocate Threadvars  }
 SysAllocateThreadVars;

 { NS : no idea what is correct to pass here - pass dummy
value for now }
 { passing a dummy is ok, the correct value is read from the
coff header of SysInstance (FK) }
 InitThread($100); { Assume everything is idempotent
there, as the thread could have been created with BeginThread... }
   end;

   if assigned(Dll_Thread_Attach_Hook) then
 Dll_Thread_Attach_Hook(DllParam);
   Dll_entry:=true; { return value is ignored }
   WinLeaveCriticalSection(AttachingThread);
end;
   DLL_THREAD_DETACH :
 begin
   declocked(Thread_count);
   if assigned(Dll_Thread_Detach_Hook) then
 Dll_Thread_Detach_Hook(DllParam);
   { Release Threadvars }
   if Win32GetCurrentThreadIdMainThreadIdWin32 then
 DoneThread; { Assume everything is idempotent there }
   Dll_entry:=true; { return value is ignored }
 end;
   DLL_PROCESS_DETACH :
 begin
   Dll_entry:=true; { return value is ignored }
   if MainThreadIDWin32=0 then // already been here.
 exit;
   If SetJmp(DLLBuf) = 0 then
 FPC_Do_Exit;
   if assigned(Dll_Process_Detach_Hook) then
 Dll_Process_Detach_Hook(DllParam);

   DoneThread;
   { Free TLS resources used by ThreadVars }
   SysFiniMultiThreading;
   WinDoneCriticalSection(AttachingThread);
   MainThreadIDWin32:=0;
 end;
 end;
  end;
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] Re: TProcess.Free - three exceptions in ntdll.dll - only *sometimes*

2010-10-05 Thread Bernd Kreuss
On 05.10.2010 20:55, Bernd Kreuss wrote:
 I see this error message now exactly once, immediately *after* the DLL
 has been unloaded by the host application.

Correction: I see it after my log output within my finalization section,
it might still be in the final stages of DLL_PROCESS_DETACH.

Where exactly is the finalization (the finalization section in my source
code) hooked into? is it Dll_Process_Detach_Hook?

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


[fpc-pascal] library (win32 dll) finalization section - what is allowed and what not?

2010-10-04 Thread Bernd Kreuss
Hi,

I'm currently fighting with some problems that seem to have to do with
the order in which things are done when a library is unloaded.

For example I have written a little exception handler unit that installs
its handler function with AddVectoredExceptionHandler() from within the
initialization section and uninstalls it in the finalization section.
Now I seem to have discovered that it matters in which order I use my
units: when I use my exception handler as the last unit in my library it
will not crash on unload but if it is the first unit it will reliably
crash with c005 somewhere in ntdll.dll (sometimes i can catch and
print it while the main application (3rd party, closed source) is still
running but sometimes the app even disappears instantly).

Also it seems that I am risking more crashes *sometimes* and totally
non-deterministic when I use things like
OutputDebugString(PChar(Format(...))) in the finalization section,
sometimes this happens even without the Format() but sometimes it works
(it will output the string but then immediately crash. Might this be the
PChar that becomes invalid too early during unload?)

Also things like TProcess.Terminate; TProcess.Free will reliably provoke
a crash when done during finalization and in general it seems that also
all my TThreads are already stopped (but not freed) at this point, but i
also can't free them anymore at this point.

Currently I am poking around in the dark and doing some trial and error
and try to leave this section alone (as much as possible). Is there
anything I could read that would tell me clearly what is actually
allowed here and in which order things are uninitialized so I can see
myself what must fail and why and what can be safely done?

Bernd

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


[fpc-pascal] TProcess.Free - three exceptions in ntdll.dll - only *sometimes*

2010-10-04 Thread Bernd Kreuss
Hi,

has anybody ever had the problem of TProcess.Free causing
three exceptions in in ntdll.dll?

C005 at 7C928FEA in C:\WINDOWS\system32\ntdll.dll

7C928FEA seems to be RtlpWaitForCriticalSection() somewhere deep inside
the windows kernel.

The first two happen almost immediately and the third one a few seconds
later. I narrowed it down to TProcess.CloseOutput(), CloseInput() and
CloseStderr() and if I create the process with poStderrToOutPut there
will only be two exceptions instead of three.

At the time this happens the process is already terminated because I
sent it its quit command via its input and then WaitOnExit() and it will
terminate orderly. Nobody should be reading or writing to these pipes
anymore.

Is there any alternative way to close the stdio? My only workaround at
the moment is to never free the TProcess at all and simply hope that it
won't crash when the dll is unloaded (it is all residing inside a dll
that is loaded and unloaded during runtime). Most of the time it won't
crash this way, but unfortunately not always.

Bernd

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


[fpc-pascal] TFileStream and stdin - how to make this work?

2010-10-03 Thread Bernd Kreuss
Hi,

this might be a stupid question but I find myself again struggling with
file IO and stdin/stdout. I am trying to do the following:

StdIn := TFileStream.Create('/dev/stdin', fmOpenRead);

and later on i want to poll whether there is something to read. I want
to try reading binary data that will be piped to my application and i
need to poll (non blocking!) and in case of incoming data read the first
byte to determine the type of message that has arrived and then read the
rest.

The Stream seems to be able to read when there is something to read on
stdin but I cannot find any way to actually test whether the ReadByte()
will block (nothing to read) or whether there is data available.

the properties size and position both are always $ and
the stream has no eof property.

Can this be done at all? And is my way of opening the stream correct?
What would be a more platform independent way of opening the stdin as a
stream to read binary data or is there a different and better way to do
this, maybe a stream is not the correct thing for this at all? How do I
poll and read binary data from stdin?

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


Re: [fpc-pascal] TFileStream and stdin - how to make this work?

2010-10-03 Thread Bernd Kreuss
On 03.10.2010 15:21, Andreas Schneider wrote:
 You could use TIOStream from the unit iostream. AFAICS that should do
 exactly what you want - i.e. opening stdin/out/err as a TStream
 descendant.
 I don't know however, if that fixes the problem you have with the
 stream blocking when nothing is to be read.

TIOStream seems to work the same way on linux and windows (I tested only
with wine but it worked) ...and it will block.

It seems I really have to do the receiving of the data in a separate
thread.

(I wanted to avoid this because the things that must be done after
reading the data must be done in the main thread (it's a GUI
application) and now I have to fiddle around with synchronize  friends
instead of a simple method call which is not nice because I also have to
immediately answer the messages on stdout in a synchronous manner with
information received from the GUI. Polling on idle from the GUI thread
would have been so much more simple and elegant.)

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


Re: [fpc-pascal] TFileStream and stdin - how to make this work?

2010-10-03 Thread Bernd Kreuss
On 03.10.2010 16:16, Honza wrote:

 See also sockets:
 
 http://wiki.lazarus.freepascal.org/Sockets
 
 and pipes:
 
 http://www.freepascal.org/docs-html/fcl/pipes/tinputpipestream.html
 
 The TInputPipeStream has a property NumBytesAvailable: DWord; [r]
 Number of bytes available for reading., which may enable the polling.

I'm currently evaluating what is the easiest (least complicated) way of
communicating with a separate executable that is only used to display
some GUI forms to display some data and a few GUI elements to give input
to the main application. It is some kind of frontend to a process that
cannot have its own GUI (the other half of my code resides in a DLL and
i haven't found any *reliable* and robust way of creating a LCL GUI
directly from within this library because I have no control over the
application's main thread, all I have are function calls from the main
application on certain events that all come from different threads).

I have a (buggy) prototype that tries to create its own GUI thread in
the DLL and an ugly mess of QueueAsyncCall() to communicate with it and
a homemade event loop but it will still randomly crash --- no idea why,
not reliable.

And I have another (already working) prototype which uses
tSimpleIPCServer and Client to communicate with a separately started
executable, this works quite nice but i thought i could make it even
easier and more compact.

Now I have a third prototype almost complete that uses TProcess to start
the exe from the dll and this is why I needed the stdio stuff. I have
now made a separate thread to listen on stdio and the synchronize()
stuff seems not so complicated as I initially thought. Overall I
currently like the TProcess/stdio method the most.

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


Re: [fpc-pascal] Re: TFileStream and stdin - how to make this work?

2010-10-03 Thread Bernd Kreuss
On 03.10.2010 19:10, Seth Grover wrote:
 Could you use THandleStream
 (http://www.freepascal.org/docs-html/rtl/classes/thandlestream.html)?
 
 myInputStream := THandleStream.Create(StdInputHandle);

Yea, this looks like what TIOStream internally seems to do. I have now
worked around the blocking issue by simply reading from the stream in a
separate thread.

I almost found the above myself (found something similar with google)
but I couldn't remember the name of the StdInputHandle constant i only
found a windows only example with something like STD_INPUT_HANDLE while
I was sitting on my Linux laptop at that moment.

(the final application will run on windows but i was doing some initial
experiments with TProcess and stdio while sitting at my linux machine)

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


[fpc-pascal] address of a label (possibly stupid question)

2010-09-05 Thread Bernd Kreuss
Hi,

I have a probably stupid question, experienced assembler programmers
will probably laugh but here is my problem:

I am trying to push the address of a label onto the stack but it doesn't
seem to work. __finally is a label and handler is the name of a
function. I can push the address of the function onto the stack without
problem but not that of the label.

The following illustrates the problem, both writelns should output the
same but in the first case with the label it won't. What am I missing?
Is this some near/far thing and how can i force it to be far? or is it
something else? The address should be  004017D5 but it is pushing the
number 00A36458 onto the stack. Why does the same syntax for a function
address work?

{$asmmode intel}

begin
  asm
push __finally// this is a label
pop eax
mov testus, eax
  end;
  writeln(IntToHex(PtrUInt(testus), 8));
  writeln(IntToHex(PtrUInt(@__finally), 8));

  asm
push handler // this is a function
pop eax
mov testus, eax
  end;
  writeln(IntToHex(PtrUInt(testus), 8));
  writeln(IntToHex(PtrUInt(@handler), 8));



Z:\Desktop\from_xp\lazproject\SEHsehtest.exe
00A36458
004017D5
0040B1D0
0040B1D0
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] asm in att mode: SECTION:DISP(BASE, INDEX, SCALE)

2010-09-05 Thread Bernd Kreuss
{$asmmode att}
asm
  pushl %fs:0
end;

Error: Invalid Reference Syntax

I am referring to this:
http://www.cs.northwestern.edu/~pdinda/ics-f09/doc/gas-notes.txt

Is this syntax wrong? I remember having seen it exactly in this form in
the .s file before I converted all my code to att syntax.

In intel syntax it was

push fs:[0]

how should this be done in att syntax?

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


[fpc-pascal] SEH

2010-09-05 Thread Bernd Kreuss
Hi,

I was bored and I played around with SEH exceptions. It might be useful
in some cases when you can't catch an exception in a dll but there might
also be easier methods in some cases. I though before the code will be
forgotten and deleted again I post it here:

the unit seh.pas contains the handler. This handler is very simple, it
will handle all exceptions and always restore the stack to the state it
was when the handler was installed and use the address that was pushed
to the stack to continue execution.

the file sehtest.pas uses this unit and sets up a try/except construct
with two asm blocks and some labels and then triggers an exception
somewhere deeper in the callstack (to test the proper restoring of the
stack). If FPC had more flexible macros then it could be made a bit
nicer but it fulfills its purpose.

Bernd
unit SEH;

{$mode objfpc}

interface

function handler(
  ExceptionRecord: Pointer;
  EstablisherFrame: Pointer;
  ContextRecord: Pointer;
  DispatcherContext: Pointer): DWORD; stdcall;

implementation
uses
  sysutils;

const // these consts are copy-pasted from system.pp
  EXCEPTION_MAXIMUM_PARAMETERS= 15;
  MAXIMUM_SUPPORTED_EXTENSION = 512;

type // these types are copy-pasted from system.pp
  PFloatingSaveArea = ^TFloatingSaveArea;
  TFloatingSaveArea = packed record
  ControlWord : Cardinal;
  StatusWord : Cardinal;
  TagWord : Cardinal;
  ErrorOffset : Cardinal;
  ErrorSelector : Cardinal;
  DataOffset : Cardinal;
  DataSelector : Cardinal;
  RegisterArea : array[0..79] of Byte;
  Cr0NpxState : Cardinal;
  end;

  PContext = ^TContext;
  TContext = packed record
  //
  // The flags values within this flag control the contents of
  // a CONTEXT record.
  //
  ContextFlags : Cardinal;

  //
  // This section is specified/returned if CONTEXT_DEBUG_REGISTERS is
  // set in ContextFlags.  Note that CONTEXT_DEBUG_REGISTERS is NOT
  // included in CONTEXT_FULL.
  //
  Dr0, Dr1, Dr2,
  Dr3, Dr6, Dr7 : Cardinal;

  //
  // This section is specified/returned if the
  // ContextFlags word contains the flag CONTEXT_FLOATING_POINT.
  //
  FloatSave : TFloatingSaveArea;

  //
  // This section is specified/returned if the
  // ContextFlags word contains the flag CONTEXT_SEGMENTS.
  //
  SegGs, SegFs,
  SegEs, SegDs : Cardinal;

  //
  // This section is specified/returned if the
  // ContextFlags word contains the flag CONTEXT_INTEGER.
  //
  Edi, Esi, Ebx,
  Edx, Ecx, Eax : Cardinal;

  //
  // This section is specified/returned if the
  // ContextFlags word contains the flag CONTEXT_CONTROL.
  //
  Ebp : Cardinal;
  Eip : Cardinal;
  SegCs : Cardinal;
  EFlags, Esp, SegSs : Cardinal;

  //
  // This section is specified/returned if the ContextFlags word
  // contains the flag CONTEXT_EXTENDED_REGISTERS.
  // The format and contexts are processor specific
  //
  ExtendedRegisters : array[0..MAXIMUM_SUPPORTED_EXTENSION-1] of Byte;
  end;

  PExceptionRecord = ^TExceptionRecord;
  TExceptionRecord = packed record
  ExceptionCode   : cardinal;
  ExceptionFlags  : Longint;
  ExceptionRecord : PExceptionRecord;
  ExceptionAddress : Pointer;
  NumberParameters : Longint;
  ExceptionInformation : array[0..EXCEPTION_MAXIMUM_PARAMETERS-1] of Pointer;
  end;

function handler(
  ExceptionRecord: Pointer;
  EstablisherFrame: Pointer;
  ContextRecord: Pointer;
  DispatcherContext: Pointer): DWORD; stdcall;

var
  exceptaddr : PDWORD;

begin
  writeln(format('exception %x', [PExceptionRecord(ExceptionRecord)^.ExceptionCode]));
  exceptaddr := EstablisherFrame + 12;  // 3 pushs earlier (the except label)
  PContext(ContextRecord)^.Eip := exceptaddr^;
  PContext(ContextRecord)^.Esp := DWORD(EstablisherFrame);
  Result := 0;
end;


end.

program sehtest;

{$mode objfpc}{$H+}
{$asmmode intel}

uses
  SEH, sysutils;


procedure foo2(foobaz: integer);
var
  x : integer;
begin
  x := 1 div foobaz;
  //PInteger(nil)^ := foobaz;
end;

procedure foo1(foobaz: integer);
var
  x : integer;
begin
  foo2(foobaz);
end;

procedure foo(foobaz: integer);
var
  x : integer;
begin
  foo1(foobaz);
end;



procedure test;
label
  __except, __finally;
begin
  writeln(IntToHex(PtrUInt(@__except), 8));

  // ** TRY ** (push the except address and ebp and then install SEH)
  asm
lea eax, __except
push eax
push ebp
push handler
push fs:[0]
mov fs:[0], esp
  end;
  // ** TRY **

  writeln('trying...');
  foo(0); // provoke exception somewhere deeper in the callstack
  goto __finally;

  __except:
  writeln('except');

  __finally:
  // ** FINALLY ** (undo the things done in try)
  asm
pop eax
mov fs:[0], eax
pop eax // handler
   

[fpc-pascal] Re: SEH

2010-09-05 Thread Bernd Kreuss
On 05.09.2010 23:00, Bernd Kreuss wrote:

 the unit seh.pas contains the handler. This handler is very simple, it
 will handle all exceptions and always restore the stack to the state it
 was when the handler was installed and use the address that was pushed
 to the stack to continue execution.

There might be still something missing: Maybe the Handler should also
pull the pushed ebp out of the stack (the same way it pulls the except
address for the instruction pointer eip) and restore it or local
variables might not work anymore after __expept.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] textmode ide configuration

2010-08-02 Thread Bernd Kreuss
Hi,

quick question: where does the textmode IDE store its configuration? It
seems I have messed up something but I cant find any configuration at
the obvious places where I would expect them.

The following are the places I would intuitively look for a config file

/etc/fp.cfg (or something starting with .fp)

/etc/fp/

~/.fp.cfg (or something starting with .fp)

~/.fp/

but i cannot find it.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


Re: [fpc-pascal] textmode ide configuration

2010-08-02 Thread Bernd Kreuss
On 02.08.2010 16:30, Marco van de Voort wrote:
 In our previous episode, Jonas Maebe said:
 quick question: where does the textmode IDE store its configuration?

 I think that by default it stores them in the current directory (to  
 support different configuration files for different projects.) The  
 names are fp.ini, fp.cfg and fp.dsk
 
 Afaik if it can find centrally stored ones, it asks if you want to use them
 or create new ones. At least it works that way on windows.

I found them, it was fp.ini, fp.cfg and fp.dsk in the current directory.
I didn't notice them because I was doing some experiments in my home dir
and i did it from the console, so I didn't see them amongst all my other
files there at a glance.

It suddenly didn't display any compiler error messages anymore and I
read somewhere that this might be a messed up configuration, so I was
searching for a global configuration file. Simply deleting the above
mentioned files solved the problem.
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal


[fpc-pascal] installing or using multiple versions of FPC on linux

2010-07-26 Thread Bernd Kreuss
Currently I have the latest svn of FPC installed (make install) which
results in all files being installed where they belong, the binary being
in the search path and fpc.cfg in /etc and I can easily use it with Lazarus.

Now I want to test something I wrote whether it will compile with an
older version of FPC and I even need to recompile the entire Lazarus
with the older FPC because it is a patch for Lazarus itself.

What is the easiest way to have a second installation of FPC around that
I can easily switch to on the same machine without having to change half
a dozen paths and moving config files around?

I have thought about switching compilers system-wide by simply having
two compiled versions in two separate directories lying around and then
simply make install in one of them or in the other one to switch
between them. Does this make sense or is there a better (recommended)
way of doing this?
___
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal