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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:09:12+00:00 2026-06-13T23:09:12+00:00

I’m using this block: procedure ExecMethod(Target: TClass; const MethodName: string; const Args: array of

  • 0

I’m using this block:

procedure ExecMethod(Target: TClass; const MethodName: string; const Args: array of TValue);
var
  LContext: TRttiContext;
  LType: TRttiType;
  LMethod: TRttiMethod;
begin
  LType := LContext.GetType(Target);
  for LMethod in LType.GetMethods do
    if (LMethod.Parent = LType) and (LMethod.Name = MethodName) then begin
      LMethod.Invoke(Target.Create, Args);
      break;
    end;
end;

like this:

ExecMethod(TFuncClass, 'Test1', []);
ExecMethod(TFuncClass, 'Test2', ['hey']);
ExecMethod(TFuncClass, 'Test3', [100]);

on this class:

  TFuncClass = class(TObject)
    published
      procedure Test1;
      procedure Test2(const str: string);
      procedure Test3(i: integer);
      // there's more, each one with different prototype
  end;

var
  FuncClass: TFuncClass;

but then, i keep getting access violations… or invalid cast pointer class (or whatever)..

  • 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-13T23:09:13+00:00Added an answer on June 13, 2026 at 11:09 pm

    As I noted at the source of your code, it leaks memory because it creates instances of the given class without ever freeing them. That shouldn’t cause any immediate run-time errors, though, so it is not the cause of the problem at hand.

    The question’s code generalizes the original code to work on any given class, and in so doing, becomes technically wrong. To see why, you need to understand how Delphi constructs objects from class references:

    When you call a constructor on a class-reference variable (as in Target.Create), the compiler uses the knowledge it as at compile time to decide which constructor to call. In this case, the target of the call is a TClass, and the only constructor the compiler knows is available for that type is TObject.Create, so that’s the constructor that’s called. If TFuncClass has some other constructor — even if it matches the zero-argument signature inherited from TObject — it’s never called. The type of the created object will still appear as TFuncClass, though — the ClassType function will return TFuncClass, and the is operator will work as expected.

    When code calls the wrong constructor on a class, it ends up with some half-valid instance of the desired class. Having invalid instances could lead to all sorts of problems. I wouldn’t be surprised if that included access violations, invalid type casts, invalid results, or whatever.

    The code shown in the question shouldn’t have the problem I’ve described, though, since TFuncClass has no new constructor. However, the given code is obviously incomplete, so maybe it’s been over-simplified for presentation here.

    You’d be much better off leaving it the responsibility of the caller to provide an instance to call methods on, like this:

    procedure ExecMethod(Target: TObject; const MethodName: string; const Args: array of TValue);
    var
      LContext: TRttiContext;
      LType: TRttiType;
      LMethod: TRttiMethod;
    begin
      LType := LContext.GetType(Target.ClassType);
      for LMethod in LType.GetMethods(MethodName) do
        // TODO: Beware of overloaded methods
        LMethod.Invoke(Target, Args);
    end;
    

    Use that function like so:

    FuncClass := TFuncClass.Create(...);
    try
      ExecMethod(FuncClass, 'Test1', []);
      ExecMethod(FuncClass, 'Test2', ['hey']);
      ExecMethod(FuncClass, 'Test3', [100]);
    finally
      FuncClass.Free
    end;
    

    Note that this is all assuming that the second parameter, the string name of the method, is actually provided by some variable whose value is unknown until run time. If you’re passing a string literal to ExecMethod, then you should stop calling ExecMethod, stop messing around with RTTI, and just call the desired method directly: FuncClass.Test2('hey').

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,

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.