Recent

Author Topic: Mutlithreaded Dll Callback crashes my Application  (Read 3769 times)

Connor

  • Newbie
  • Posts: 3
Mutlithreaded Dll Callback crashes my Application
« on: August 24, 2010, 04:32:52 pm »
Hi,

I am trying to use multithreaded dll for network communication in lazarus. I've ported a component
from Delphi which works fine there and get a crashing app with lazarus.
I've tracked down the problem to a callback function which is called by a thread from the dll.

I don't have any clue how to solve this problem at the moment and need help.

I've provided a demo project together with a dll to reproduce the problem.

My Versions (again)

Lazarus IDE v0.9.28.2 Beta
MS Windows XP Prof DE sp3
FPC Version 2.2.4

What I basically do is passing a pointer to a calback funtion to a dll
writte in c++ (Visual Studio 2005) and call it from there.
** The follwing code is from the example attached to this mail **

procedure OnExecuteClb(pCBData : Pointer); cdecl;
begin
...
end;

The function is passed to the dll like this

...
    FHandle    := uVCMTDLL.vcmtdll_NewThread();
    uVCMTDLL.vcmtdll_RegisterCallback(FHandle,@OnExecuteClb,self);
...


The test includes a dll which has the following interface

...
#define VCMTDLL_API __declspec(dllexport)

namespace VCMTDLL {

typedef void* MT_THREAD_HANDLE;

extern "C" {

VCMTDLL_API MT_THREAD_HANDLE vcmtdll_NewThread();  // create a new
thread object
VCMTDLL_API void vcmtdll_DeleteThread(MT_THREAD_HANDLE ahandle); //
delete an existing thread object
VCMTDLL_API void vcmtdll_RegisterCallback(MT_THREAD_HANDLE ahandle,
void* pCBFunc, void* pCBData); // pass a callback to execute inside the
thread context
VCMTDLL_API void vcmtdll_StartThread(MT_THREAD_HANDLE ahandle, unsigned
int iDelay); // start the execution with a delay of <iDelay> milliseconds

}// extern "C"

...

with a loop in the callback function everything is fine

...
   x := 0;
   while x < 100 do
   begin
     x := x + 1;
   end;
...

I can execute it without problems.

BUT if I create and destroy an object inside the procedure the app
crahes or hangs after several runs of the test (between 1 an 10 starts).

the object:

...
TVCMTDummyObject = class
private
public
      Tag : Integer;
end;
...

the code in the callback

...
   a := TVCMTDummyObject.Create;
   a.Free;
...

Another problem is that a PostMessage from the callback's thread context
immediatly crashes the app. But this may be resolved later.

If there is anybody who uses a similar mechanism in his lazarus project
and knows how to solve this problem it'd be great.
The demo project together with dll is attached too.

Best Regards
Connor

Connor

  • Newbie
  • Posts: 3
Re: Mutlithreaded Dll Callback crashes my Application
« Reply #1 on: August 25, 2010, 02:15:46 pm »
I found something in the lazarus package for vortex :

{ FPC specific thread create function to replace Vortex's thread create.
This is required because FPC doesn't work when C libraries create their own
threads}

This may lead to a solution I guess.
Some more background about that would be nice. Is this documented anywhere ?

Connor