I have following code (I checked objCur is not Nil before free}:
try
objCur.Free;
Except on E:Exception do
begin
OutputDebugString(PChar('Exception '+E.Message));
Assert(False);
end;
end;
It report this exception message:
Invalid pointer operation. objCur is TXX_TEA type.
objCur: TXX_TEA;
In TXX_TEA.Destroy I have following code
destructor TXX_TEA.Destroy;
begin
OutputDebugString(PChar('Inside Destroy'));
...
inherited;
OutputDebugString(PChar('End of Destroy'));
end;
In debugView I see following messages:
Inside Destroy
…
End of DestroyException: Invalid pointer operation
I know objCur.Free calls TXX_TEA.Destroy, but it looks TXX_TEA.Destroy executes without error. So where should I trace this invalid pointer operation?
An invalid pointer operation occurs when the memory manager is asked to release memory that doesn’t belong to it.
An object’s memory is freed just before the outermost destructor returns to the caller. The caller in this case is
TObject.Free. Callinginheriteddoes not cause an object’s memory to be freed because the compiler knows that it’s not the outermost call.Evidently, you’re freeing an object that doesn’t really exist, but the contents of the memory for this supposed object look valid enough that the code in the destructor that cleans up object’s fields doesn’t crash. It’s only when the destructors finish running, and the object is going to be freed, that the memory manager detects that the address doesn’t refer to anything currently allocated.