PDA

View Full Version : Weird EInvalidPointer Exception



pstudio
16-06-2011, 03:13 PM
Language: Delphi XE

So I'm trying out some stuff and I run into EInvalidPointer Exception: 'Invalid Pointer Operation'. However I can't find out where I'm doing something wrong.



destructor TPEngine.Destroy;
var
Screen: IPScreen;
begin // <------------ Works up until here
// Clean up Screens
while Screens.Count > 0 do
begin
Screen := Screens.Last;
Screens.Remove(Screen);
end;
Screens.Free;

inherited;
end;


Screens is a List and will always hold at least one Screen. An instance of TPEngine is created in the units initialization section and freed in the finalization section as the last executable code in the program. If there's only one Screen in the Screens list everything works fine. The Exception is raised when I add another screen.
The debugger gets to the begin keyword in the destructor. If I take one step the exception will be raised.

As I can see it the exception is raised before my code is executed so I have no idea what causes the exception. Does anyone have an idea what may cause this exception in this case?

User137
17-06-2011, 12:23 AM
I'm guessing TPEngine itself was freed already and so would be using code that has no owner.