Reputation:
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)..
Upvotes: 1
Views: 1427
Reputation: 163247
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')
.
Upvotes: 2