MyObject is another variable from the FMyObject field. And you are only nil in the FMyObject field.
FreeAndNil frees the object that it points to, and nil the variable you passed. It does not magically detect, and nil all other variables point to the freed object.
FreeAndNil(FMyObject); does the same thing:
object(FMyObject).Free(); FMyObject=nil;
(Technically, this is not entirely correct; casting an object to an object is reinterpreted due to the untyped var parameter, but it does not matter here)
And this, obviously, only modifies FMyObject , not MyObject
Oh, I just noticed that you are hiding the original Free method? This is madness. FreeAndNil still uses the original Free . This will not hit you in your example, because you are calling Free on a variable with the static type TBigObject , not FreeAndNil . But this is a receipt for the disaster.
Instead, you should override the Destroy destructor.
source share