[Lazarus] Mutlithreaded Dll Callback crashes my Application

Michael Schnell mschnell at lumino.de
Tue Aug 3 11:58:46 CEST 2010


  On 08/03/2010 10:40 AM, Maik Wojcieszak wrote:
> Hi,
>
> I'm working on a port of a Delphi component to Lazarus. The component 
> wraps a dll for Network Communication
> based on ASPLs Vortex Library but implementing a full set of "easy to 
> use" mechanisms for "daily work problems".
>
> The dll itself is written in C++ with a C like interface. It is 
> multithreaded and uses callbacks for a lot of purposes.
> The callback function/procedure is defined like
OK a "flattened" interface is necessary.
>
> //#---------------------------------------------------------------------
> //                     CALLBACK PROCEDURES
> //#---------------------------------------------------------------------
> procedure ExecuteCommandClb(tmlhandle : TML_COMMAND_HANDLE; pCBData : 
> Pointer); cdecl;
> begin
>  //...
> end;
Did you define cdecl specification both in C and in Pascal ? AFAIK, with 
M$ Studio this is "__declspec(dllexport)", I don't know what the GNU 
notation for this is.

You also can use STDCALL instead of cdecl on both sites. I seem to 
remember the GNU C notation is __STDCALL, but this might be wrong.

>
> No matter what I do inside this procedure if I reach the end the 
> assembler window is popping up in the debugger.
> The Parameters seem to be correct.
>
> pCBData is a Pointer to an object stored with the pointer to the 
> callback. The procedure is used as a proxy to call
> the real worker method of the object.
You can't use C++ objects in Pascal. You need to flatten the interface 
completely by explicitly communicating addresses of flat functions to do 
a callback. This can be quite complicated :(.
>
> tmlhandle is also a Pointer, but to an object that is implemented in 
> the dll.
Also pointers need to be "flat" and not point to C++ (or FPC) objects.

Of course a multithreaded DLL offers some even more advanced problems. I 
would do a test with a singlethreaded DLL just to test all aspects of 
the interface to your FPC project and activate the threads later.

It should be no problem if a thread in a DLL calls a callback to your 
Pascal project. (We do this with Delphi and a PJ-SIP DLL.) But of course 
such a callback can't do any "visual" LCL stuff (like writing to the 
screen). Here you need to store the information (e.g. in a FIFO  done 
with a TThreadList) and use the mainthread to do the LCL based stuff.

-Michael





More information about the Lazarus mailing list