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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:44:17+00:00 2026-06-03T14:44:17+00:00

How can I track the count of a certain class in memory in Delphi

  • 0

How can I track the count of a certain class in memory in Delphi 7, without adding a static counting member in the class.
For the purpose of tracking the program performance.
Thank you in advanced.

  • 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-03T14:44:19+00:00Added an answer on June 3, 2026 at 2:44 pm

    You can hook the NewInstance and FreeInstance methods in the class VMT:

    unit ClassHook;
    
    {no$DEFINE SINGLE_THREAD}
    
    interface
    
    var
      BitBtnInstanceCounter: integer;
    
    implementation
    
    uses Windows, Buttons;
    
    function GetVirtualMethod(AClass: TClass; const VmtOffset: Integer): Pointer;
    begin
      Result := PPointer(Integer(AClass) + VmtOffset)^;
    end;
    
    procedure SetVirtualMethod(AClass: TClass; const VmtOffset: Integer; const Method: Pointer);
    var
      WrittenBytes: {$IF CompilerVersion>=23}SIZE_T{$ELSE}DWORD{$IFEND};
      PatchAddress: PPointer;
    begin
      PatchAddress := Pointer(Integer(AClass) + VmtOffset);
      WriteProcessMemory(GetCurrentProcess, PatchAddress, @Method, SizeOf(Method), WrittenBytes);
    end;
    
    {$IFOPT W+}{$DEFINE WARN}{$ENDIF}{$WARNINGS OFF} // avoid compiler "Symbol 'xxx' is deprecated" warning
    const
      vmtNewInstance = System.vmtNewInstance;
      vmtFreeInstance = System.vmtFreeInstance;
    {$IFDEF WARN}{$WARNINGS ON}{$ENDIF}
    
    type
      TNewInstanceFn = function(Self: TClass): TObject;
      TFreeInstanceProc = procedure(Self: TObject);
    
    var
      OrgTBitBtn_NewInstance: TNewInstanceFn;
      OrgTBitBtn_FreeInstance: TFreeInstanceProc;
    
    function TBitBtn_NewInstance(Self: TClass): TObject;
    begin
      Result := OrgTBitBtn_NewInstance(Self);
      {$IFDEF SINGLE_THREAD}
      Inc(BitBtnInstanceCounter);
      {$ELSE}
      InterlockedIncrement(BitBtnInstanceCounter);
      {$ENDIF}
    end;
    
    procedure TBitBtn_FreeInstance(Self: TObject);
    begin
      {$IFDEF SINGLE_THREAD}
      Dec(BitBtnInstanceCounter);
      {$ELSE}
      InterlockedDecrement(BitBtnInstanceCounter);
      {$ENDIF}
      OrgTBitBtn_FreeInstance(Self);
    end;
    
    procedure InstallHooks;
    begin
      OrgTBitBtn_NewInstance := GetVirtualMethod(TBitBtn, vmtNewInstance);
      OrgTBitBtn_FreeInstance := GetVirtualMethod(TBitBtn, vmtFreeInstance);
      SetVirtualMethod(Buttons.TBitBtn, vmtNewInstance, @TBitBtn_NewInstance);
      SetVirtualMethod(Buttons.TBitBtn, vmtFreeInstance, @TBitBtn_FreeInstance);
    end;
    
    procedure RemoveHooks;
    begin
      SetVirtualMethod(Buttons.TBitBtn, vmtNewInstance, @OrgTBitBtn_NewInstance);
      SetVirtualMethod(Buttons.TBitBtn, vmtFreeInstance, @OrgTBitBtn_FreeInstance);
    end;
    
    initialization
      InstallHooks;
    
    finalization
      RemoveHooks;
    
    end.
    

    Include this unit in any uses clause of your program and now the BitBtnInstanceCounter will track the count of TBitBtn instances.

    Edit: if it is possible that several threads simultaneously create objects of the tracked class, it is necessary to use interlocked access to modify the counter variable. Beware that third-party components could silently use threads, so it’s safer to not define the SINGLE_THREAD symbol.

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

Sidebar

Related Questions

I'm getting reports of memory leaks in my app, but I can't track down
To put in other words: How can a class track whether its constructor is
I know you can track a svn repo with git by using git svn
I know that Mercurial can track renames of files, but how do I get
Is there any way that I can track the connections made to my application's
I need a unique request ID for my logger, so I can track each
How can you track the movement of a JFrame itself? I'd like to register
How can I track the browser idle time? I am using IE8. I am
An odd error here, perhaps someone can help track down source as it's attempting
I'm getting a strange exception in Xcode 4.2.1 (ARC-enabled project) that I can't track

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.