Hallo,

When I caught an exception in methode Execute and re-raise it
in a synchronised TThreadMethode, the RTL tries to free the 
Exception-Objekt twice. Does anybody have some tip what's wrong
with my code? Thanks in advance.

procedure TWorkerThread.Execute;
[..]
  if Assigned(FJobObj) and (not Terminated) then
  begin
    FJobObj.FThread := Self;
    try
      try
        FJobObj.Execute;
        raise Exception.Create('TestTest');
      except
        on E: Exception do
        begin
          if Assigned(FJobObj) and (not Terminated) then
          begin
            FJobObj.FLastException := E;
            SyncResult;
          end;
        end;
      end;
    finally
      { Following conditions return false, }
      { and Execute is exited (FreeOnTerminate = TRUE)   }
      if Assigned(FJobObj) and (not Terminated) then
        SyncResult;
      if Terminated then
        FreeAndNil(FJobObj);
    end;
  end;
[..]

procedure TWorkerThread.SyncResult;
begin
  if Assigned(FJobObj.FOnResult) then
    Synchronize(TriggerResult);
end;

procedure TWorkerThread.TriggerResult;
begin
  FJobObj.FOnResult(FJobObj, FJobObj.FLastException);
end;

procedure TForm1.JobResults(AJobObj: TJobObject; E: Exception);
begin
  try
    { Now the developer is a fool and he re-raises the Exception,    }
    { causes the RTL to free the exception object twice,  see trace  }
    {  below. If I create and throw a new exception there won't be a }
    { problem. }
    if E <> nil then
      raise E;
  finally
      { Needs to be freed here! Since the destruktor will nil }
      { object variabel "FJobObj" of the thread, important! }
    AJobObj.Free;
  end;
end;


--------------------------------2008/4/1 
14:20:46--------------------------------
FastMM has detected an attempt to call a virtual method on a freed object.
An access violation will now be raised in order to abort the current 
operation.

Freed object class: Exception

Virtual method: Destroy

Virtual method address: 403830

The allocation number was: 505

Stack trace of when the object was allocated (return addresses):
402D58 [EMAIL PROTECTED]
4037E7 [system.pas][System][TObject.NewInstance][8360]
403B6A [EMAIL PROTECTED]
4105EA [sysutils.pas][SysUtils][Exception.Create][13251]
463AD8 [Pooltest1.pas][Pooltest1][TMyJob.Execute][50]
462E91 [ThreadPool.pas][ThreadPool][TWorkerThread.Execute][344]
7C91E21F [ZwQueryVirtualMemory]
7C80B9BD [VirtualQueryEx]
7C80B9E6 [VirtualQuery]

Stack trace of when the object was subsequently freed (return addresses):
402D83 [EMAIL PROTECTED]
403805 [system.pas][System][TObject.FreeInstance][8366]
403BB5 [EMAIL PROTECTED]
40383E [system.pas][System][TObject.Destroy][8379]
40384B [system.pas][System][TObject.Free][8385]
403FBC [EMAIL PROTECTED]
7C9137BF [RtlConvertUlongToLargeInteger]
7C91378B [RtlConvertUlongToLargeInteger]
7C947B48 [RtlUnwind]

The current stack trace leading to this error (return addresses):
40384B [system.pas][System][TObject.Free][8385]
40405F [EMAIL PROTECTED]
420690 [classes.pas][Classes][TThread.Synchronize][9599]
7C9137BF [RtlConvertUlongToLargeInteger]
7C91378B [RtlConvertUlongToLargeInteger]
7C947860 [LdrAddRefDll]
7C9137BF [RtlConvertUlongToLargeInteger]
7C91EAFA [KiUserExceptionDispatcher]
7C812A5B [RaiseException]

--
Arno Garrels 

_______________________________________________
Delphi mailing list -> [email protected]
http://lists.elists.org/cgi-bin/mailman/listinfo/delphi

Reply via email to