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

  • SEARCH
  • Home
  • 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 186791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:42:32+00:00 2026-05-11T15:42:32+00:00

For one particular issue in the architecture of an application I’m working on, interfaces

  • 0

For one particular issue in the architecture of an application I’m working on, interfaces seem to be a nice solution. Specifically, some ‘business objects’ depend on a bunch of settings that are pulled from the database in the actual app. Letting those business objects ask for an interface (through Inversion of Control), and letting a central TDatabaseSettings object implement those interfaces, allows for better isolation, and thus for much easier unit testing.

However, in Delphi, interfaces seem to come with an, in this case, unpleasant bonus: reference counting. This means that if I do something like this:

type IMySettings = interface     function getMySetting: String; end;  TDatabaseSettings = class(..., IMySettings)     //... end;  TMyBusinessObject = class(TInterfacedObject, IMySettings)     property Settings: IMySettings read FSettings write FSettings; end;  var   DatabaseSettings: TDatabaseSettings;      // global object (normally placed in a controller somewhere)  //Now, in some function... O := TMyBusinessObject.Create; O.Settings := DatabaseSettings;  // ... do something with O O.Free; 

On the last line (O.Free), my global DatabaseSettings object is now also freed, since the last interface reference to it (which was contained in O) is lost!

One solution would be to store the ‘global’ DatabaseSettings object with an interface; another solution would be to override the reference counting mechanism for the TDatabaseSettings class, so I can continue to manage the DatabaseSettings as a normal object (which is much more consistent with the rest of the app).

So, in summary, my question is: how do I disable the interface reference counting mechanism for a particular class?

I’ve been able to find some info that suggests overriding the IInterface methods _AddRef and _Release for the class (TDatabaseSettings in the example); has anyone ever done that?

Or would you say I shouldn’t do this (confusing? just a bad idea?), and find a different solution to the architectural problem?

Thanks a lot!

  • 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. 2026-05-11T15:42:33+00:00Added an answer on May 11, 2026 at 3:42 pm

    Ok, you can bypass it, but the question is if you really want that. If you want to use interfaces, you better use them completely. So as you have experienced it, you get problems if you mix class and interface variables.

    var   // DatabaseSettings: TDatabaseSettings;    DatabaseSettings : IMySettings;  //Now, in some function... O := TMyBusinessObject.Create; O.Settings := DatabaseSettings;  // ... do something with O O.Free; 

    You now have a second reference to the interface and losing the first will not free the object.

    It as also possible to keep both the class and the object:

    var   DatabaseSettings: TDatabaseSettings;    DatabaseSettingsInt : IMySettings; 

    Be sure to set the interface right after the object has been created.

    If you really want to disable reference counting, you just have to create a new descendant of TObject that implements IInterface. I have tested the example below in D2009 and it works:

    // Query Interface can stay the same because it does not depend on reference counting. function TMyInterfacedObject.QueryInterface(const IID: TGUID; out Obj): HResult; begin   if GetInterface(IID, Obj) then     Result := 0   else     Result := E_NOINTERFACE; end;  constructor TMyInterfacedObject.Create; begin   FRefCount := 1; end;  procedure TMyInterfacedObject.FreeRef; begin   if Self = nil then     Exit;   if InterlockedDecrement(FRefCount) = 0 then     Destroy;     end;  function TMyInterfacedObject._AddRef: Integer; begin   Result := InterlockedIncrement(FRefCount); end;  function TMyInterfacedObject._Release: Integer; begin   Result := InterlockedDecrement(FRefCount);   if Result = 0 then     Destroy; end; 

    FreeRef just lowers the refcount just like _Release. You can use it where you normally use Free.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, z-index is working for one particular element in IE8, but not
I need some advice on how to go about a particular issue I'm trying
I've begun working on a new application and I decided to start this one
I'm having trouble with one particular issue, I was hoping someone could help me
I'm having a bit of trouble with one particular issue using JPA/Spring: How can
I'm trying out Plastic SCM but I'm struggling with one particular issue. I would
I have one particular problem, I have script which should issue bill statement. Now
I'm having a performance issue in my Excel macro. One particular piece of code
I have one particular FLA that is crashing every time I try to compile
How to show and hide one particular column in Mouse Over and Mouse out

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.