Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7739875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T08:38:01+00:00 2026-06-01T08:38:01+00:00

To provide as much information as I can, here’s a very basic example of

  • 0

To provide as much information as I can, here’s a very basic example of what I’m doing

type
  IMyInterface = interface
  [THE_GUID_HERE]
    // some methods
  end;

  TMyInterfaceArray = Array of IMyInterface;

  TMyInterfacedObject = class(TInterfacedObject, IMyInterface)
    // implementation of the Interface etc. here
  end;

  TContainingObject = class
  private
    FIObjectArray: TMyInterfaceArray;
  public
    constructor Create;
    destructor Destroy; override;
    procedure NewInstanceOfInterfacedObject;
  end;

  implementation

  constructor TContainingObject.Create;
  begin
    inherited;
    // Just to illustrate that an Instance is being created...
    NewInstanceOfInterfacedObject;
  end;

  destructor TContainingObject.Destroy;
  var
    I: Integer;
  begin
    for I := Low(FIObjectArray) to High(FIObjectArray) do
      FIObjectArray[I] := nil;
    SetLength(FIObjectArray, 0); // Array collapsed

    inherited;
  end;

  procedure TContainingObject.NewInstanceOfInterfacedObject;
  var
    LIndex: Integer;
  begin
    LIndex := Length(FIObjectArray);
    SetLength(FIObjectArray, LIndex + 1);
    FIObjectArray[LIndex] := TMyInterfacedObject.Create;
  end;

Okay, so an instance of TContainingObject is created, and in turn creates an instance of TMyInterfacedObject, stored in an Array of IMyInterface.

When TContainingObject‘s destructor is called, it nil’s the reference and collapses the Array.

The issue I have is that, with no other references anywhere, TMyInterfacedObject‘s destructor is never called, and thus memory leaks.

Am I doing something wrong, or is Delphi’s reference counting system not able to cope with the simple concept of Interfaced Objects being held in an Array of the Interface Type?

Thanks for any advice!

MORE INFORMATION

TContainingObject provides an Array property to access individual instances of IMyInterface contained within the Array.

In my actual code, there are circular references between multiple Interface types.
Let’s suggest that IMyInterface contains a function GetSomething: IAnotherInterface, and IAnotherInterface contains GetMyInterface: IMyInterface (a circular reference).
Could this be causing my problem? If so, the circular reference is absolutely required, so what would a solution be with that in mind?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T08:38:02+00:00Added an answer on June 1, 2026 at 8:38 am

    If the implementation for IMyInterface contains an IAnotherInterface member, and the implementation for IAnotherInterface contains an IMyInterface member, and they refer to each other, then their reference counts will never be able to fall to 0 unless you clear one of the references, which likely means adding methods to your interfaces to do that, eg:

    type
      IAnotherInterface = interface;
    
      IMyInterface = interface
      ['{guid}']
        function GetAnotherInterface: IAnotherInterface;
        procedure SetAnotherInterface(Value: IAnotherInterface);
        property AnotherInterface: IAnotherInterface read GetAnotherInterface write SetAnotherInterface;
      end;
    
      IAnotherInterface = interface
      ['{guid}']
        function GetMyInterface: IMyInterface;
        procedure SetMyInterface(Value: IMyInterface);
        property MyInterface: IMyInterface read GetMyInterface write SetMyInterface;
      end;
    

    .

    type
      TMyInterface = class(TInterfacedObject, IMyInterface)
      private
        FAnotherInterface: IAnotherInterface;
      public
        function GetAnotherInterface: IAnotherInterface;
        procedure SetAnotherInterface(Value: IAnotherInterface);
      end;
    
      TAnotherInterface = class(TInterfacedObject, IAnotherInterface)
      private
        FMyInterface: IMyInterface;
      public
        function GetMyInterface: IMyInterface;
        procedure SetMyInterface(Value: IMyInterface);
      end;
    
      function TMyInterface.GetAnotherInterface;
      begin
        Result := FAnotherInterface;
      end;
    
      procedure TMyInterface.SetAnotherInterface(Value: IAnotherInterface);
      begin
        if FAnotherInterface <> Value then
        begin
          if FAnotherInterface <> nil then FAnotherInterface.SetMyInterface(nil);
          FAnotherInterface := Value;
          if FAnotherInterface <> nil then FAnotherInterface.SetMyInterface(Self);
        end;
      end;
    
      function TAnotherInterface.GetMyInterface: IMyInterface;
      begin
        Result := FMyInterface;
      end;
    
      procedure TAnotherInterface.SetMyInterface(Value: IMyInterface);
      begin
        if FMyInterface <> Value then
        begin
          if FMyInterface <> nil then FMyInterface.SetAnotherInterface(nil);
          FMyInterface := Value;
          if FMyInterface <> nil then FMyInterface.SetAnotherInterface(Self);
        end;
      end;
    

    Now watch the reference counts when you don’t explicitally free one of the references:

    var
      I: IMyInterface;
      J: IAnotherInterface;
    begin
      I := TMyInterface.Create; // I.RefCnt becomes 1
      J := TAnotherInterface.Create; // J.RefCnt becomes 1
      I.AnotherInterface := J; // I.RefCnt becomes 2, J.RefCnt becomes 2
      ...
      {
      // implicit when scope is cleared:
      I := nil; // I.RefCnt becomes 1, I is NOT freed
      J := nil; // J.RefCnt becomes 1, J is NOT freed
      }
    end;
    

    Now add an explicit release to one of the references:

    var
      I: IMyInterface;
      J: IAnotherInterface;
    begin
      I := TMyInterface.Create; // I.RefCnt becomes 1
      J := TAnotherInterface.Create; // J.RefCnt becomes 1
      I.AnotherInterface := J; // I.RefCnt becomes 2, J.RefCnt becomes 2
      ...
      I.AnotherInterface := nil; // I.RefCnt becomes 1, J.RefCnt becomes 1
      {
      // implicit when scope is cleared:
      I := nil; // I.RefCnt becomes 0, I is freed
      J := nil; // J.RefCnt becomes 0, J is freed
      }
    end;
    

    .

    var
      I: IMyInterface;
      J: IAnotherInterface;
    begin
      I := TMyInterface.Create; // I.RefCnt becomes 1
      J := TAnotherInterface.Create; // J.RefCnt becomes 1
      I.AnotherInterface := J; // I.RefCnt becomes 2, J.RefCnt becomes 2
      J := nil; // I.RefCnt still 2, J.RefCnt becomes 1, J is NOT freed yet
      ...
      I.AnotherInterface := nil; // I.RefCnt becomes 1, J.RefCnt becomes 0, J is freed
      {
      // implicit when scope is cleared:
      I := nil; // I.RefCnt becomes 0, I is freed
      }
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Much like how the video tag can provide multiple source attributes so an mp4
interfaces provide a useful abstraction capability. One can have a class Foo implement some
I hope you will bear with me. I wanted to provide as much information
I have a very strange issue, that I'm hoping someone here can help me
Provide an example for the pseudo-regex: Match every url except those from example.com and
please provide me the possibility of my issue. Can we customize the push notification
how can I provide feedback for a shell command that may run for quite
I have been developing and tweaking things as much as I can and know
Background First of all, much gratitude to atebits for their very informative blog post
Please provide a good, thorough tutorial about Java class loading , focusing on how

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.