Hi!
I would like that some kind of Method like
- TObject.SetAfterconstructionCallback (MyCallback: Procedure with self parameter) could be set in the object and be called if not null/enabled.
Same for Before Destruction (like TObject.SetBeforeDestructionCallback (MyCallback: Procedure with self parameter) )
And their pairs of disabling it.
Why?
Because for debugging needs it is really interesting to have the possibility of a Callback here, because All Objects inherits from TObject in historical code.
The Self information gives a good entry point for debugging routines and high level routines to show actual system consumption/order of resource compsuption. And not a fixed photo.
TVirtualMethod interceptors seems weirder
https://docwiki.embarcadero.com/Libraries/Rio/en/System.Rtti.TVirtualMethodInterceptor and not implemented.
Intercepting the memory manager can be done, but you only get when a heap memory is needed (and their quantity), not the class info.
How is actually done?
One can derive a Class like TDebugObject=TObject, and then descend from these, but it's useless for existing code. So the Derive first paradigm is of no application/use.
Other ways?
program Project1;
{$mode objfpc}{$H+}
uses classes;
procedure MyAfterConstruction(self: TObject);
begin
writeln('AfterConstruction: ', self.ClassName);
end;
var o: TObject;
ovmt: PVmt;
p: TPersistent;
l: TList;
begin
ovmt:=PVmt(TObject);
ovmt^.vAfterConstruction := @MyAfterConstruction;
ovmt:=PVmt(TPersistent);
ovmt^.vAfterConstruction := @MyAfterConstruction;
ovmt:=PVmt(TList);
ovmt^.vAfterConstruction := @MyAfterConstruction;
o := TObject.create;
p := TPersistent.Create;
l := TList.Create;
end.
But this is weird, doesn't work in windows, and bad programming style (hacking). (well, works if one calls windows to unprotect memory). And this is not a portable way of doing things.
A method for callback INMHO its the right way OR a controlled (by method/procedure in FPC) to redirect de AfterConstruction / Before construction calls...
It could even be enforced with compiler directives if needed (i think it is desirable)
And for object oriented programming gives a powerful tool for bookeeping/accounting classes and teaching if all it's not done ok.
As a compiler implementation, i ask devs if this is possible and if could be done. I really think it's very useful with current TObject implementation.