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 7191643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:46:21+00:00 2026-05-28T19:46:21+00:00

I have a helper class that will be in wide use across the application.

  • 0

I have a helper class that will be in wide use across the application. The implementation relies on interface reference counting, the idea roughly is:

...
var
  lHelper: IMyHelper;
begin
  lHelper := TMyHelper.Create(some params);
  ...some code that doesn't have to access lHelper
end;

So the implementation relies on IMyHelper going out of scope at the end of the method, but not before.

So what am asking is, can I be certain that in some future Delphi compiler won’t play smart and release the interface right after it’s created if the variable is not accessed in rest of the method ?

  • 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-05-28T19:46:22+00:00Added an answer on May 28, 2026 at 7:46 pm

    IMHO you can be confident of that. The out-of-scope pattern will probably remain global to the instruction block of this method. This would be a breaking change.

    See this comment from Barry Kelly (from Embarcadero):

    As to your earlier comment, about explicit variables: in the hypothetical (and breaking change) case, where we optimized interface variable usage, we would likely not only break the described RAII-like functionality but also the explicit variable approach too; the values assigned to FooNotifier and BarNotifier are not used, so “in theory” they can be freed up sooner, and potentially even reuse the same storage.

    But of course, destruction of the interface can have side-effects, and that’s what’s being relied upon for the effect in the post. Changing the language such that side-effects like these have visible changes is not something we do willingly.

    So you can guess that Embarcadero won’t introduce any backward compatibility change here. The benefit of re-using an interface memory won’t be worth breaking compatibility and introducing side effects: saving a pointer (4 or 8 bytes) is not worth it nowadays, especially when the stack is already allocated, and aligned (x64 model uses more stack than x86).

    Only if a Garbage Collector is introduced to the language (which I do not want from my personal point of view), objects life time may change. But in this case, life time may probably be longer.

    In all cases, you can create your own code, to be sure that it will released at the end of the method:

    var
      lHelper: IMyHelper;
    begin
      lHelper := TMyHelper.Create(some params);
      try 
        ...some code that doesn't have to access lHelper
      finally
        lHelper := nil; // release the interface count by yourself
      end;
    end;
    

    In fact, this is the code already generated by the compiler. Writing this will be perfectly redundant, but it will ensure that compiler won’t cheat on you.

    When speaking of interfaces and reference counting, please take in account the potential issue of circular references in Delphi. See this great article (i.e. “Example 2-15”) about the need of “weak pointers” for circular references of Interfaces.

    Other languages (like Java or C#) use a garbage collector to resolve this. Objective C uses an explicit “zeroing weak pointers” mechanism to solve it – see this discussion or this SO answer for a potential implementation. Perhaps future version of Delphi may consider using an implementation similar to the ARC model introduced in Objective C. But I suspect there will be an explicit syntax to preserve compatibility with existing code.

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

Sidebar

Related Questions

I have a helper class that is just a bunch of static methods and
I have a helper class pulling a string from an XML file. That string
I have a base class for many tests that has some helper methods they
I have a helper class that implements ITypedList, to provide objects for databinding against
I have an I18n helper class that can find out the available Locale s
I have a C# helper class ( ExcelMacroHelper ) that assists with injecting VBA
I have an interface based class that I want to have a few static
I have the following methods in an enum helper class (I have simplified it
I have some ASP.NET web services which all share a common helper class they
I currently have a class file with the following enumeration: using System; namespace Helper

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.