Recent

Author Topic: TObject AfterConstruction / Before Destruction callback methods.  (Read 3016 times)

damieiro

  • Full Member
  • ***
  • Posts: 200
TObject AfterConstruction / Before Destruction callback methods.
« on: September 22, 2021, 12:34:46 pm »
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?

Code: Pascal  [Select][+][-]
  1.     program Project1;
  2.     {$mode objfpc}{$H+}
  3.     uses classes;
  4.      
  5.     procedure MyAfterConstruction(self: TObject);
  6.     begin
  7.        writeln('AfterConstruction: ', self.ClassName);
  8.     end;
  9.      
  10.     var o: TObject;
  11.       ovmt: PVmt;
  12.       p: TPersistent;
  13.       l: TList;
  14.     begin
  15.       ovmt:=PVmt(TObject);
  16.       ovmt^.vAfterConstruction := @MyAfterConstruction;
  17.       ovmt:=PVmt(TPersistent);
  18.       ovmt^.vAfterConstruction := @MyAfterConstruction;
  19.       ovmt:=PVmt(TList);
  20.       ovmt^.vAfterConstruction := @MyAfterConstruction;
  21.       o := TObject.create;
  22.       p := TPersistent.Create;
  23.       l := TList.Create;
  24.     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.


« Last Edit: September 22, 2021, 12:41:21 pm by damieiro »

damieiro

  • Full Member
  • ***
  • Posts: 200
Re: TObject AfterConstruction / Before Destruction callback methods.
« Reply #1 on: September 23, 2021, 07:38:58 pm »
To mods: Please, move this to suggestions: I think it's the right forum. thx in advance.

 

TinyPortal © 2005-2018