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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:01:09+00:00 2026-05-27T21:01:09+00:00

I’m trying to clone objects using RTTI in D2010. Here’s my attempt so far:

  • 0

I’m trying to clone objects using RTTI in D2010. Here’s my attempt so far:

uses SysUtils, TypInfo, rtti;
type
  TPerson = class(TObject)
  public
    Name: string;
    destructor Destroy(); Override;
  end;
destructor TPerson.Destroy;
begin
  WriteLn('A TPerson was freed.');
  inherited;
end;
procedure CloneInstance(SourceInstance: TObject; DestinationInstance: TObject; Context: TRttiContext); Overload;
var
  rSourceType:      TRttiType;
  rDestinationType: TRttiType;
  rField:           TRttiField;
  rSourceValue:     TValue;
  Destination:      TObject;
  rMethod:          TRttiMethod;
begin
  rSourceType := Context.GetType(SourceInstance.ClassInfo);
  if (DestinationInstance = nil) then begin
    rMethod := rSourceType.GetMethod('Create');
    DestinationInstance := rMethod.Invoke(rSourceType.AsInstance.MetaclassType, []).AsObject;
  end;
  for rField in rSourceType.GetFields do begin
    if (rField.FieldType.TypeKind = tkClass) then begin
      // TODO: Recursive clone
    end else begin
      // Non-class values are copied (NOTE: will cause problems with records etc.)
      rField.SetValue(DestinationInstance, rField.GetValue(SourceInstance));
    end;
  end;
end;
procedure CloneInstance(SourceInstance: TObject; DestinationInstance: TObject); Overload;
var
  rContext:       TRttiContext;
begin
  rContext := TRttiContext.Create();
  CloneInstance(SourceInstance, DestinationInstance, rContext);
  rContext.Free();
end;
var
  Original:     TPerson;
  Clone:        TPerson;
begin
  ReportMemoryLeaksOnShutdown := true;
  Original := TPerson.Create();
  CloneInstance(Original, Clone);
  Clone.Free();
  Original.Free();
  ReadLn;
end.

A little disappointingly, I don’t see more than one occurrence of “A TPerson was freed.’ to the output (which is confirmed by stepping through the program) – only the original is destroyed using the overridden destructor.

Can anyone please help me having the overridden destructor called? (And perhaps explain why it isn’t called in the first place.) Thanks!

  • 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-27T21:01:10+00:00Added an answer on May 27, 2026 at 9:01 pm

    Couple of problems with your code.

    You do not initialize the Clone variable to nil. Which on my machine led to access violations in the upper CloneInstance method, as no clone was created because the passed in value was non-nil.

    You do not have the DestinationInstance parameter declared as var. This means that the instantiation in the upper CloneInstance method doesn’t get back to the caller. Adding var to the parameter solves the problem. You do need to use TObject(Clone) in the call to CloneInstance from the main method of the program, or Delphi will complain about ‘there is no overloaded method that can be called with these parameters’. This is because var parameters want their exact declared type passed into them.

    I changed your code to:

    uses
      SysUtils,
      TypInfo,
      rtti;
    
    type
      TPerson = class(TObject)
      public
        Name: string;
        constructor Create;
        destructor Destroy(); Override;
      end;
    
    constructor TPerson.Create;
    begin
      WriteLn('A TPerson was created');
    end;
    
    destructor TPerson.Destroy;
    begin
      WriteLn('A TPerson was freed.');
      inherited;
    end;
    
    procedure CloneInstance(SourceInstance: TObject; var DestinationInstance: TObject; Context: TRttiContext); Overload;
    var
      rSourceType:      TRttiType;
      rDestinationType: TRttiType;
      rField:           TRttiField;
      rSourceValue:     TValue;
      Destination:      TObject;
      rMethod:          TRttiMethod;
    begin
      rSourceType := Context.GetType(SourceInstance.ClassInfo);
      if (DestinationInstance = nil) then begin
        rMethod := rSourceType.GetMethod('Create');
        DestinationInstance := rMethod.Invoke(rSourceType.AsInstance.MetaclassType, []).AsObject;
      end;
      for rField in rSourceType.GetFields do begin
        if (rField.FieldType.TypeKind = tkClass) then begin
          // TODO: Recursive clone
        end else begin
          // Non-class values are copied (NOTE: will cause problems with records etc.)
          rField.SetValue(DestinationInstance, rField.GetValue(SourceInstance));
        end;
      end;
    end;
    
    procedure CloneInstance(SourceInstance: TObject; var DestinationInstance: TObject); Overload;
    var
      rContext:       TRttiContext;
    begin
      rContext := TRttiContext.Create();
      CloneInstance(SourceInstance, DestinationInstance, rContext);
      rContext.Free();
    end;
    
    var
      Original:     TPerson;
      Clone:        TPerson;
    begin
      Clone := nil;
      ReportMemoryLeaksOnShutdown := true;
      Original := TPerson.Create();
      Original.Name := 'Marjan';
    
      CloneInstance(Original, TObject(Clone));
      Original.Name := 'Original';
      WriteLn('Original name: ', Original.Name);
      WriteLn('Clone name: ', Clone.Name);
    
      Clone.Free();
      Original.Free();
      ReadLn;
    end.
    

    I added a constructor to see both instances being created as well and a couple of lines to check the names after the cloning. The output reads:

    A TPerson was created
    A TPerson was created
    Original name: Original
    Clone name: Marjan
    A TPerson was freed.
    A TPerson was freed.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.