I'm trying to find a multi-thread deadlock, that somehow seems to have to do with my file logging (if I comment that out, the code doesnt deadlock anymore).
When it is deadlocked, I get this screen from the Delphi debugger on "Pause":
Does this mean anything to anyone? Is this some "typical", "recognizable" code?
The file writing looks like this (and is called from many threads):
procedure TLogging.WriteLogEntry(EntryText:string);
begin
FFileLock.BeginWrite;
try
if not Assigned(FFilestream) then begin
if FileExist(LogName) then begin
FFilestream := TFileStream.Create(LogName, fmOpenReadWrite+fmShareDenyWrite);
end else begin
FFilestream := TFileStream.Create(LogName, fmCreate+fmShareDenyWrite);
end;
end;
if Assigned(FFilestream) and not Assigned(FExporter) then begin
FExporter := TStreamWriter.Create(FFilestream, TEncoding.UTF8);
FExporter.BaseStream.Seek(0, soFromEnd);
FExporter.NewLine := #$0A;
FExporter.AutoFlush := True;
end;
FExporter.Write('['+DateToStr(Now, FDateTimeFormat)+'] ['+TimeToStr(Now, FDateTimeFormat)+'] [#'+Lead0(GetCurrentThreadId, 5)+']: '+EntryText);
FExporter.WriteLine;
FreeAndNIL(FFilestream);
FreeAndNIL(FExporter);
finally
FFileLock.EndWrite;
end;
end;
FFileStream
and FExporter
started as global variables in this object, but I have moved them into this method because I need to close the log file each time. FFileLock
is a TMultiReadExclusiveWriteSynchronizer
inside TLogging
, and my application has only one instance of TLogging
.