I see... The trick was to define TMyClass!
Thanks!
On 2/1/24 02:19, Michael Van Canneyt via fpc-pascal wrote:
On Wed, 31 Jan 2024, Amir--- via fpc-pascal wrote:
Without more info (declaration of ChildTClass, declaration of the
constructor
of that class etc), it is not possible to comment.
We need a complete compilable code sample to provide you with more
insight.
Please have a look at the attachment.
The constructor of TObject is not virtual, in difference with the
destructor.
Given the definitions
ChildObj: TObject;
ChildTClass: TClass;
The following statement
ChildObj := ChildTClass.Create;
will always use the TObject.Create, since it is not virtual.
In general, you cannot use TObject for this kind of thing.
For this to work, you need to create a descendent of TObject with a
virtual
constructor (call it TMyObject) of known signature, and do
ChildObj: TMyObject;
ChildTClass: TMyClass; // class of TMyobject
then
ChildObj := ChildTClass.Create;
will take the correct overridden constructor.
Your code is also incomplete in the sense that it will only work for
classes
with a constructor without arguments. If a overloaded constructor
exists which takes arguments (like TComponent), it will not be called
and the class will not be instantiated correctly.
But maybe that will not be your use-case.
I attached a version of your program that works and has no memleaks.
Michael.
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
_______________________________________________
fpc-pascal maillist - fpc-pascal@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal