[fpc-devel] FPC shared lib called from C shared lib called from C main program crashes

Lukas Zeller luz_info at synthesis.ch
Thu Sep 17 16:16:15 CEST 2009


Hello Jonas,

thanks for responding so quickly!

On Sep 17, 2009, at 11:50 , Jonas Maebe wrote:

> On 17 Sep 2009, at 11:33, Lukas Zeller wrote:
>
>> I guess I really need to understand how the FPC runtime expects to  
>> be initialized when located in a shared lib, but I couln't find any  
>> hint so far in the FPC docs nor in the list archives.
>
> Which FPC version are you using? I fixed shared library  
> initialisation/finalisation for Mac OS X and Linux in FPC 2.2.4.

I am using FPC 2.2.4 on Mac OS X and the problem happens there.

The linux version runs on an external server, I'll ask the people  
running it there what FPC version they have and if the program really  
crashes without the Write(' ').

> The only things you still have to watch out for are
> a) don't pass ansistrings/widestrings/other reference counted types  
> from one FPC shared library to another (they use different memory  
> managers, so you'll get crashes when one tries to free memory  
> allocated by another), or alternatively make sure that all libraries  
> use the "cmem" memory manager

Yes, I read about this in the archives. This can't be the problem as  
there are only plain C strings (PChar) passed between the C part and  
the FPC shared lib.

> b) don't use these libraries in multi-threaded applications. It is  
> possible to get them to work, but hard. See below for a text I  
> posted last year to the mac-pascal list. It should still apply  
> though. It was also about applications rather than libraries, but  
> simply put the initialisation code in the initialisation section of  
> your library rather than in the "begin..end." block of the main  
> program.

The application is single-threaded. But thanks for the information  
about thread init altough it can't be the problem here.

So it must be something more profane.

And in fact it was - while re-testing everything for writing this  
email, by accident I found the following:

The Write in the library initialisation is required if any other part  
of the library wants to write to stdout later as part of the normally  
called library functions. As soon as there is no single Write or  
WriteLn called anywhere in the library, it works.

So for practical purposes of our project, the problem is solved (we  
don't want any stdout from the library anyway, the ones that caused  
the crash were quick hacks inserted for debugging).

For technical curiosity however I still wonder what interals of the  
runtime a Write or WriteLn in the library init code initializes  
correctly while the same Write called later in a normal library  
function makes the program crash.

More precisely, I think in the later call to Write it still tries to  
initialize something, but then has side effects on other data  
structures in the runtime, which for example make memory allocations  
crash - note that the Write/WriteLn themselves WORK, i.e. show on the  
console, but make the program crash shortly afterwards (as said, my  
observation was during allocation of a TObject, although the debugging  
situation was a bit difficult so I could be mistaken here).

Best Regards,

Lukas






More information about the fpc-devel mailing list