Runtime Error 217 on multithreaded app termination
Original Reporter info from Mantis: abb
-
Reporter name: Alexander Bokovikov
Original Reporter info from Mantis: abb
- Reporter name: Alexander Bokovikov
Description:
My XCode ObjC project uses FPC static library. I use a project template, developed by Jonas Maebe in 2009 for FPC 2.3.1.
The problem appears when I include cmem uinit into the uses clause of main library file. My app uses threads. And the problem appears only if at least one additional thread has been started (I use TThread descendents). If it might make a sense, thread is created in the initialization section of a unit. Never tried to create threads after initialization is done.
The bug is 100% reproducible. You can see the call stack screenshot attached. I've set the FPC_RAISEEXCEPTION breakpoint in my XCode project, otherwise GDB was terminated with a message about exception "EInvalidHandle".
Steps to reproduce:
- Create a XCode ObjC Cocoa project with FPC static library or just use Jonas Maebe template.
Library PascalLibrary;
uses cthreads, cmem, Math, FPCUnit;
exports
MyFPCProc;
begin
SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide, exOverflow, exUnderflow, exPrecision]);
end.
- Make sure cmem is in uses clause of the library source file.
- Create a unit with a very simple TThread descendent like this:
unit FPCUnit;
interface
uses Classes, SysUtils;
procedure MyFPCProc(var Result : integer); cdecl;
implementation
procedure MyFPCProc(var Result : integer);
begin
Result := 0;
end;
type
TTestThread = class(TThread)
protected
procedure Execute; override;
end;
function usleep(value : DWORD) : integer; cdecl; external;
procedure TTestThread.Execute;
begin
while not Terminated do usleep(50000);
end;
var
tt : TTestThread;
initialization
tt := TTestThread.Create(true);
tt.FreeOnTerminate := false;
tt.Priority := tpIdle;
tt.Start;
finalization
tt.Terminate;
tt.WaitFor;
tt.Free;
end.
- Run the project and then just close the application. RunTime error 217 should appear in the GDB log window.
Additional information:
The bug doesn't depend on how exactly thread is terminated. I tried to use WaitFor, or just a cycles with usleep() of several kinds. The key factor is just cmem unit presence.
This bug was initially detected in FPC 2.4.2.
This bug is missing in FPC 2.2.4.
Have no information about versions between 2.2.4 and 2.4.2.
Really I don't need cmem unit in my project. I've discovered this bug due to my carelessness -- this unit was used in the initial project template, and I just didn't remove it from the uses clause.
Mantis conversion info:
- Mantis ID: 19620
- OS: Mac OS X
- OS Build: 10.5.8
- Build: 2.4.4 [2011/05/01]
- Platform: Intel
- Version: 2.4.3