title: Delphi - invalid pointer operation when closing the application from the taskbar menu
body: <p>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.</p>
  
  <p>the code responsible for destroying the datamodules </p>
  
  <pre><code>  for iPos := 0 to FDatamodules.Count - 1 do //FDataModules is of type TList
      if FDatamodules.Items[iPos] &lt;&gt; nil then 
        TDatamodule(FDatamodules.Items[iPos]).Free;
  </code></pre>
  
  <p>and the stack</p>
  
  <pre><code>: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
  </code></pre>
  
  <p>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. </p>
  
  <p><img src="https://i.stack.imgur.com/tixCK.png" alt="enter image description here"></p>