[Lazarus] Log4D weirdness

Mario R. Carro mcarro at gmx.net
Fri Dec 19 22:49:07 CET 2014


Hello everyone, I've been debgging a weird problem I was having with a 
little app of mine. I'm using the latest stable Lazarus and FreePascal. 
The same project on Windows runs fine. On Linux (Debian Sid) fires an 
exception when running the finalization code.

I narrowed the problem to the use of Log4D (svn head from SF). Simply 
adding Log4D to the uses list is enough to make the application fail on 
exit.

Long-history-short, the culprit code is this:

constructor TLogLog.Create;
begin
   inherited Create('');
   Appender := ;
   AddAppender(TLogODSAppender.Create(''));
   InternalDebugging := False;
   Level             := Log4D.Debug;
end;

There's a LogLog variable that is destroyed in the finalization section 
(and fails). The problem is that the TLogODSAppender created in the ctor 
ends up destroyed inside the AddAppender call. This is the AddAppender code:

procedure TLogLogger.AddAppender(const Appender: ILogAppender);
begin
   LockLogger;
   try
     if FAppenders.IndexOf(Appender) = -1 then
     begin
       FAppenders.Add(Appender);
       if FHierarchy <> nil then
         FHierarchy.FireAppenderEvent(True, Self, Appender);
     end;
   finally
     UnlockLogger;
   end;
end;

The appender dtor is called inside the call to IndexOf (it seems by the 
stack trace because it's use-count reaches zero). So: The appender is 
destroyed before is adding it to the list and later the TLogLog dtor 
fails when tries to destroy it again.

I already solved the problem, but don't know why. Changing the 
TLogLog.Create code by this one makes the problem disappear:

constructor TLogLog.Create;
var
   Appender: ILogAppender;
begin
   inherited Create('');
   Appender := TLogODSAppender.Create('');
   AddAppender(Appender);
   InternalDebugging := False;
   Level             := Log4D.Debug;
end;

Who is at fault here? Log4D? The compiler? Me?
Any hints will be appreciated...

/MRC




More information about the Lazarus mailing list