[Lazarus] Mutlithreaded Dll Callback crashes my Application

Maik Wojcieszak maikwo at googlemail.com
Tue Aug 3 15:02:22 CEST 2010


Alexander,

This is the result of the test:

G:\Workdir HEAD\cb Test\thr_test>project1.exe
Main ThreadID=864
Creating external thread
External is called - ThreadID=6000
blub
param=test
TSomeClass.someMethod called

Freeing external thread

G:\Workdir HEAD\cb Test\thr_test>

it's ok, isn't it ?

Maik

Alexander Grau schrieb:
> Maik,
>
> I have written the counterpart of the "textthr.pp"  test (I mentioned 
> before) for Win32 below - it works for my compiler (release 2.4.0 ). 
> Maybe you want to try it on your version (2.2.4) too.
>
> Now I think your problem is more related to something inside the DLL 
> (just another assumption). Try to do just a simple call inside your 
> callback to that DLL, and do not create or pass FPC strings 
> ('someString') and do not call anything from the FPC runtime 
> (assigned(x)).  If that it still does work, you have some call 
> parameter issues. If it works, try to use more and more from the 
> runtime (e.g. try to use PChar strings instead of strings)....
>
> Regards,
> Alexander
>
>
>
> program project1;
>
> {$APPTYPE CONSOLE}
>
> {$mode objfpc}{$H+}
>
>
> uses
>  {$IFDEF UNIX}{$IFDEF UseCThreads}
>  cthreads,
>  {$ENDIF}{$ENDIF}
>  Classes,
>  { you can add units after this }
>  windows;
>
> {$R project1.res}
>
> type
>  TSomeClass = class
>  public
>    procedure someMethod();
>  end;
>
> procedure TSomeClass.someMethod();
> begin
>  writeln('TSomeClass.someMethod called');
> end;
>
>
> var
>  ThreadID: DWORD;       //Thread-ID
>  ThreadHandle: THandle; //Rückgabewert von CreateThread
>  someObj: TSomeClass;
>
>
> procedure someFunc(param: pchar); cdecl;
> begin
>  WriteLn('External is called - ThreadID=', GetCurrentThreadId);
>  if assigned(param) then writeln('blub');
>  writeln('param=', param);
>  someObj.someMethod();
> end;
>
> function ExternalThread(zahl: Pointer): LongInt; stdcall;
> begin
>  //Sleep(2000);
>  someFunc('test');
>  Result:=0;
> end;
>
> begin
>  WriteLn('Main ThreadID=', GetCurrentThreadId);
>  someObj:=TSomeClass.create;
>
>  WriteLn('Creating external thread');
>  ThreadHandle:=CreateThread(nil, 0, 
> TFNThreadStartRoutine(@ExternalThread),
>    nil, 0, ThreadID);
>  if ThreadHandle = 0 then writeln('ERROR creating external thread');
>  readln;
>
>  WriteLn('Freeing external thread');
>  if ThreadHandle<>0 then CloseHandle(ThreadHandle);
>
>  someObj.free;
> end.
>
>
>
>
>
>
> Maik Wojcieszak schrieb:
>> Alexander,
>>
>>> Try the following to limit the problem:  in your callback do not 
>>> call *ANY* FPC runtime related code - Just a "Win32 MessagBox(0, 
>>> "hi", "text", MB_OK)".  Add the process id (GetProcessId) to that 
>>> dialog output and also in your main program (to see if you are 
>>> really in an external thread).
>>>
>> I am in an external thread.
>>> If that works, call a simple global FPC function (no object) and 
>>> again just that callback there.
>>>
>>> After that works, just a simple FPC object call and again that 
>>> message box.
>>>
>>> I still assume in the compiler version you are using, you simply 
>>> can't call FPC objects inside external threads.
>> this is the full code of the callback
>>
>> procedure ExecuteCommandClb(tmlhandle : TML_COMMAND_HANDLE; pCBData : 
>> Pointer); cdecl;
>> var
>>  hsidex : SIDEX_HANDLE;
>>  val    : SIDEX_VARIANT;
>> begin
>>  tml_Cmd_Get_Sidex_Handle(tmlhandle, at hsidex);
>>  if assigned(hsidex) then
>>  begin
>>    val := sidex_Variant_New_Boolean(true) ;
>>    sidex_Variant_Write(hsidex,'Answer', 'Accept', val);
>>    sidex_Variant_DecRef(val);
>>  end;
>> end;
>>
>> no FPC objects are called. I've thought that this might be the reason 
>> and changed the code to try, but it didn't work either.
>> All calls are API calls of my dll.
>>>
>>> Also have a look at the mantis - e.g. see if 0012987 is related to 
>>> your problem? (http://bugs.freepascal.org/view.php?id=12987)
>> This sounds pretty much like my problem.
>> I'm new to lazarus and I've downloaded the installer package as it 
>> is. Can I change the FPC Compiler without changing my current IDE and 
>> how can I do this ?
>> I think it might be a good idea to use the current version to double 
>> check if the problem is still there.
>> The latest release is 2.4.0. Why does the lazarus installer for 
>> windows install 2.2.4, it seems  a little bit out of date ?
>>>
>>>
>>> I might be fully wrong with my assumptions, I'm just thinking loudly 
>>> because I had the same issue - just another architecture :-)
>> I am happy about any hint to solve this one. The dll is already 
>> ported to Linux and Mac OS-X and I've decided to use lazarus for
>> some demo applications.
>>
>> Maik
>>
>> -- 
>> _______________________________________________
>> Lazarus mailing list
>> Lazarus at lists.lazarus.freepascal.org
>> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>>
>
>
> -- 
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
>





More information about the Lazarus mailing list