Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - invalid pointer operation when closing the application from the taskbar menu

I have an application which is loading all dynamically created datamodules into a TList. Application is running under windows XP OS. When closing the application from the application close button the code executes correctly. But when closing the application from the taskbar menu an Invalid pointer operation is raised. The same code behaves different when closing the application in different ways.

the code responsible for destroying the datamodules

  for iPos := 0 to FDatamodules.Count - 1 do //FDataModules is of type TList
    if FDatamodules.Items[iPos] <> nil then 
      TDatamodule(FDatamodules.Items[iPos]).Free;

and the stack

:7c812a6b kernel32.RaiseException + 0x52
System.TObject.FreeInstance
System.ErrorAt(2,$4A7FEFC)
System.Error(reInvalidPtr)
System.TObject.FreeInstance
System._ClassDestroy(???)
Classes.TDataModule.Destroy
System.TObject.Free
RBAFORM.TRBABaseForm.Destroy

LE: It seems that closing the application from the taskbar menu is sending a HALT(0) to the application and the datamodule get freed. On the image bellow :on the left is the stack of the normal close action, on the right the stack of closing application from the taskbar menu.

enter image description here

like image 445
RBA Avatar asked Oct 27 '25 20:10

RBA


1 Answers

This seems to be a double free problem (like LU commented on you question) which means that you are freeing an instance of you data module more then once.

When you create an instance with Create(nil) it's not owned by anyone and you can safely call Free on it. (See also What is the meaning of nil owner in component constructor)

If you create it with Create(Form1) (non nil argument) then it's no longer your responsibility to call Free. It will be automatically freed when Form1 is freed in this example.

To be sure what's going on you should use FastMM in FullDebugMode or a similiar memory debugger. A memory debugger should be able to catch the problem and provide you with more helpful information.

like image 130
Jens Mühlenhoff Avatar answered Oct 31 '25 00:10

Jens Mühlenhoff