[fpc-pascal] Get TMethod from function reference

Hairy Pixels genericptr at gmail.com
Thu Sep 15 15:30:51 CEST 2022



> On Sep 15, 2022, at 12:28 PM, Sven Barth <pascaldragon at googlemail.com> wrote:
> 
> As mentioned elsewhere function references are in fact interfaces. And you can't retrieve a method pointer to a interface function, cause relying on that would result in either memory leaks or premature release of the interface, because TMethod only contains a Pointer for the Data field that does not support managed types.
> 
> You can get a reference to the raw interface by doing this however:
> 
> === code begin ===
> 
> var
>   proc: reference to procedure;
>   i: IUnknown;
> begin
>    i := IUnknown(PPointer(@proc)^);
> end.
> 
> === code end ===
> 
> Though I don't know what you hope to achieve with this...

Ok so what’s happening is I had a deferred dispatch library that used many different callback types and now I want to unify it using references. Problem is, with the old design I used “of object” types so I was able to get the target class and use it do to things like cancel all actions that were associated with a particular class. 

I don’t understand how memory loss could be in play. If the function reference is stored and not freed it should have the information needed to invoke a method and in theory you should be able to retrieve it anytime, providing the interface had a public method for this (which would be a nice extension to add). How is that not correct?

Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list