Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-09

Could someone explain me which the correct triggering of the BeforeDestruction method?

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

My question at title seems stupid, but the reason why I've get this doubt is just because it get different results between Free Pascal and Delphi in the following example:

=== begin code ===

program project1;
{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$IFDEF MSWINDOWS}
  {$APPTYPE CONSOLE}
{$ENDIF}
uses SysUtils;
type
  TSomeClass = class
  public
    procedure LoadSomething;
  end;
  TA = class
  private
    FSomeObject: TSomeClass;
  public
    constructor Create; virtual;
    destructor Destroy; override;
    procedure AfterConstruction; override;
    procedure BeforeDestruction; override;
    property SomeObject: TSomeClass read FSomeObject;
  end;
procedure TSomeClass.LoadSomething;
begin
  raise Exception.Create('An exception loading something');
end;
constructor TA.Create;
begin
  WriteLn(1);
  inherited Create;
end;
destructor TA.Destroy;
begin
  WriteLn(2);
  inherited Destroy;
end;
procedure TA.AfterConstruction;
begin
  WriteLn(3);
  FSomeObject := TSomeClass.Create;
  FSomeObject.LoadSomething;
end;
procedure TA.BeforeDestruction;
begin
  WriteLn(4);
  FSomeObject.Free;
end;
var
  VA: TA;
begin
  VA := TA.Create;
  try
  finally
    VA.Free;
  end;
end.

=== end code ===

I've compiled it in Free Pascal from trunk (version 3.1.1 [2016/08/26] for x86_64) installed on a Xubuntu 16.04 64 bits, and got the following log:

$ ./project1
1
3
2
An unhandled exception occurred at $00000000004001CD: Exception: An exception loading something
$00000000004001CD LOADSOMETHING, line 30 of project1.lpr $000000000040045A AFTERCONSTRUCTION, line 50 of project1.lpr $00000000004002CC CREATE, line 38 of project1.lpr $00000000004004FA main, line 62 of project1.lpr

and I've compiled the same code in an original version of Delphi Pro (version 10.1 Berlin 24.0.22858.6822), on a Windows 10 64 bits, and I got the log below and a critical exception (notice the result numbers, they are different from similar test compiled in Free Pascal):

Win32\Debug>Project1.exe
1
3
4
2
^C

(I need to kill the app by Ctrl+C within terminal)

the critical exception was the known:

[Main Instruction]
Project1.exe has stopped working

[Content]
Windows is checking for a solution to the problem...

So, is it a bug? If so, in which compiler?! Even after reading some documents about BeforeDestruction I still have doubt about which is the correct triggering of this method, and I would be glad if someone could help me to solve it.

Silvio Clécio

Reply by email

Luiz Americo Pereira Camara
to silvioprog

Seems that Delphi considers that the object is already created at the time AfterConstruction is called, so it understands that when it gets into AfterConstruction is sucessfully created. Semantically makes sense.

The counter proof is see if AfterConstruction is called when a exception occurs in constructor

Luiz

"silvioprog" wrote 126 lines
Hello, My question at title seems stupid, but the reason why I've get this doubt is just because it get different results between Free Pascal and Delphi in the following example: === begin code === program project1; {$IFDEF FPC} {$MODE DELPHI} {$ENDIF} {$IFDEF MSWINDOWS} {$APPTYPE CONSOLE} {$ENDIF} uses SysUtils; type TSomeClass = class public procedure LoadSomething; end; TA = class private FSomeObject: TSomeClass; public constructor Create; virtual; destructor Destroy; override; procedure AfterConstruction; override; procedure BeforeDestruction; override; property SomeObject: TSomeClass read FSomeObject; end; procedure TSomeClass.LoadSomething; begin raise Exception.Create('An exception loading something'); end; constructor TA.Create; begin WriteLn(1); inherited Create; end; destructor TA.Destroy; begin WriteLn(2); inherited Destroy; end; procedure TA.AfterConstruction; begin WriteLn(3); FSomeObject := TSomeClass.Create; FSomeObject.LoadSomething; end; procedure TA.BeforeDestruction; begin WriteLn(4); FSomeObject.Free; end; var VA: TA; begin VA := TA.Create; try finally VA.Free; end; end. === end code === I've compiled it in Free Pascal from trunk (version 3.1.1 [2016/08/26] for x86_64) installed on a Xubuntu 16.04 64 bits, and got the following log: $ ./project1 1 3 2 An unhandled exception occurred at $00000000004001CD: Exception: An exception loading something $00000000004001CD LOADSOMETHING, line 30 of project1.lpr $000000000040045A AFTERCONSTRUCTION, line 50 of project1.lpr $00000000004002CC CREATE, line 38 of project1.lpr $00000000004004FA main, line 62 of project1.lpr and I've compiled the same code in an original version of Delphi Pro (version 10.1 Berlin 24.0.22858.6822), on a Windows 10 64 bits, and I got the log below and a critical exception (notice the result numbers, they are different from similar test compiled in Free Pascal): Win32\Debug>Project1.exe 1 3 4 2 ^C (I need to kill the app by Ctrl+C within terminal) the critical exception was the known: [Main Instruction] Project1.exe has stopped working [Content] Windows is checking for a solution to the problem... So, is it a bug? If so, in which compiler?! Even after reading some documents about BeforeDestruction I still have doubt about which is the correct triggering of this method, and I would be glad if someone could help me to solve it. -- Silvio Clécio _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

silvioprog
to Luiz Americo Pereira Camara

On Sun, Sep 4, 2016 at 5:23 PM, Luiz Americo Pereira Camara <

Show quoted text 9 lines
wrote: Seems that Delphi considers that the object is already created at the time AfterConstruction is called, so it understands that when it gets into AfterConstruction is sucessfully created. Semantically makes sense. The counter proof is see if AfterConstruction is called when a exception occurs in constructor

And I'm even still not sure about BeforeDestruction triggering, FPC triggers 132, Delphi 1342. :-/

Silvio Clécio

Reply by email

silvioprog
to Maciej Izak
Maciej Izak wrote 8 lines
2016-09-05 17:56 GMT+02:00 silvioprog : > FPC triggers 132, Delphi 1342. :-/ Please report on bug tracker. IMO in presented context - definitely bug.

Sweet, I'm going to do it tonight. :-)

Silvio Clécio

Reply by email