[Lazarus] Mutlithreaded Dll Callback crashes my Application

Maik Wojcieszak maikwo at googlemail.com
Tue Aug 24 13:41:51 CEST 2010


Hi,

After my holiday I've done done more research on this problem and hope I
can get an answer while continuing this thread.

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, at 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
Maik









-------------- next part --------------
A non-text attachment was scrubbed...
Name: cb Test.zip
Type: application/x-zip-compressed
Size: 107105 bytes
Desc: not available
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20100824/8b587cb6/attachment-0004.bin>


More information about the Lazarus mailing list