Undocumented Delphi 8 Language Changes 3

What is the difference in behaviour in the code shown below in Delphi 7, and Delphi 8? Hint: IInterface and Inheritance (Click if you have Delphi 8 installed). Let's discuss, shall we?

The following code uses a coding habit of mine, which, incidentally, matches with that of Nick as discussed in his “Uses Clauses” article. Great minds think... [I leave that for you to complete ;o) ]

For more interesting usages of interfaces, refer to Malcolm's excellent “Interfaces: Off the Beaten Track” and his blog. Interestingly, I've used Malcolm's trick about a year ago in a VEP project.

{$APPTYPE CONSOLE}

uses
  SysUtils
{$IF DEFINED(CLR)}
  , System.Diagnostics
{$ELSEIF DEFINED(MSWINDOWS)}
  , Windows
{$IFEND}
;

type
  ISiteName = interface
  end;

  TSiteName = class(TInterfacedObject, ISiteName)
  private
    FMethodName: string;
  public
    constructor Create(MethodName: string);
    destructor Destroy; override;
  end;

function SiteName(MethodName: string): ISiteName;
begin
  Result := TSiteName.Create(MethodName);
end;

procedure DebugWriteLine(S: string);
begin
  {$IF DEFINED(CLR)}
    WriteLn(S); // Debug.Write(S);
  {$ELSEIF DEFINED(MSWINDOWS)}
    WriteLn(S); // OutputDebugString(PChar(S));
  {$IFEND}
end;

constructor TSiteName.Create(MethodName: string);
begin
  inherited Create;
  FMethodName := MethodName;
  DebugWriteLine('Enter '+FMethodName);
end;

destructor TSiteName.Destroy;
begin
  DebugWriteLine('Leave '+FMethodName);
  inherited;
end;

procedure Test;
var
  I: Integer;
begin
  SiteName('Test');
  for I := 1 to 10 do
    DebugWriteLine(IntToStr(I));
end;

begin
  Test;
end.

Published Fri, 28 May 2004 @ 7:00 AM by chuacw
Filed under:

Comments

# re: Undocumented Delphi 8 Language Changes 3

Friday, May 28, 2004 3:33 PM by chuacw

Lifetime management of interfaces is very different in D7 Win32 and D8 .NET. So the old trick og using interfaces to automatically perform resource cleanup no longer works in D8.

On .NET the GC is responsible for cleaning up all objects, inlcuding those referenced by interfaces. There is no reference counting going on. D7 has compiler magic to automatically maintain the interface's reference count and to free the implementing object (via _Release) when the last reference is removed.

# re: Undocumented Delphi 8 Language Changes 3

Monday, May 31, 2004 12:05 AM by chuacw

It's a pity that the Delphi Team didn't implement interfaces cleanup, or reference counting on Delphi 8, so now, that's one more $IFDEF to handle for the .Net platform.

Also, the weakest thing on the Delphi product has always been the handling of $IFDEFs. Since when could the IDE (editor, or whatever) handle $IFDEFs properly? Either it couldn't insert stuff in the right location, or something.

# re: Undocumented Delphi 8 Language Changes 3

Wednesday, June 02, 2004 7:46 AM by chuacw

First, if you want people to discus the results of your code, you should post the results. Don't make people wonder what your point is.

Introducing reference counting into the .NET environment would be disasterous. Nothing in .NET would know about the reference count semantic, so it would be completely unreliable. Reference counting techniques also suffer from circular reference headaches. .NET's GC does not.

What you're pointing out is a change to the underlying implementation, not the Delphi language. The Delphi language rules for interfaces are the same in D7 and D8: That interfaces will be disposed of automatically when they are no longer referenced. In D7, that's implemented using reference counted codegen. In D8, that's implemented using .NET GC services. Your destructor code is the anomaly in .NET. Be glad that we still compile it at all.

# re: Undocumented Delphi 8 Language Changes 3

Friday, June 04, 2004 3:58 AM by chuacw

Thanks for your advice Danny. It's very much appreciated.

I will endeavour to apply your advice to future articles requiring discussions.

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Enter the following code to ensure that your comment reaches the intended party:
Enter the numbers you see in the image: