I have an array that stores pointers to some objects and I want to know if it’s possible to free an object if you have only a pointer to it.
For example:
var test_form :Tform;
p: Pointer;
vartest := Tform.create(nil);
p:=@vartest;
and if at runtime I only have p is it possible to free vartest?
The pointer
ppoints to aTObjectvariable. That is,pis a pointer to a reference to an object. Therefore you need to de-referencep. Like this:p^. Becausepis un-typed you need to cast it before calling the Free method.So, putting this together, you can free the object like this:
This will not modify the
vartestvariable though. It will merely destroy the object. If you want to setvartesttonilas well then you can do it like this:FreeAndNilis an odd beast because it takes an un-typed parameter. That’s why you don’t need to cast toTObject.To avoid all this casting it would be preferable to declare p as a typed pointer, e.g.
^TObjector^TForm.