1

I know about DisposeOf (described in many places including this SO answer) to free the Form, but consider it as a hack. From what I understand it puts the object in Zombie state and might not completely remove it from memory.

The correct way would be to identify and remove all references to the Form. However trying to do this, there is one reference I cannot find. Here is my code:

var MyForm: TMyForm;
...
//Dynamically create the form
MyForm := TMyForm.Create(Application)
...
//Destroy the form to free up memory
Application.RemoveComponent(MyForm);//reduces RefCount from 4 to 2
if MyForm.RefCount=1 then //RefCount is 2 here
  MyForm := nil
else
  ShowMessage('Cannot release MyForm: RefCount='+inttostr(MyForm.RefCount));

I use Delphi 10 Seattle and target the iPad with iOS 9.0.

Where do I find the last reference to the form?

Community
  • 1
  • 1
Hans
  • 2,220
  • 13
  • 33
  • You have to use `DisposeOf` on forms, just like on any other `TComponent` descendant. You can find elaborate answer [How to free a component](http://stackoverflow.com/q/27818697/4267244) – Dalija Prasnikar Oct 09 '15 at 11:01
  • `DisposeOf` is needed because of `TComponent` notification system. Even if you at some point manage to find all circular references to your form, changes in code beyond your control (FMX) can suddenly take another "unexpected" reference and your code will be broken again. And you may not even notice that in time. `DisposeOf` is sort of a hack, but it is unavoidable hack, it is deliberately introduced hack. It does not pay off trying to fight against it. – Dalija Prasnikar Oct 09 '15 at 11:08
  • @DalijaPrasnikar Ok, if it is an unavoidable hack then I will use DisposeOf. Thank you for your feedback. – Hans Oct 09 '15 at 11:14
  • I can't see any hack here. After calling `TObject.DisposeOf` you will find `TObject.Disposed` on `true` http://docwiki.embarcadero.com/Libraries/en/System.TObject.DisposeOf You can get a notification whenever a component is freed/disposed [`TComponent.FreeNotification`](http://docwiki.embarcadero.com/Libraries/en/System.Classes.TComponent.FreeNotification) – Sir Rufo Oct 09 '15 at 12:06
  • @SirRufo Hack because it breaks ARC rule "every object reference should point to either valid object or nil". – Dalija Prasnikar Oct 09 '15 at 14:55
  • @DalijaPrasnikar No rule breaking at all ... you still have a **valid** reference and not a dangling pointer. The instance is just disposed ... **nearly the same** with a DB connection component when you close the connection - DisposeOf will release the internal used resources but is still alive (not really usable of course) – Sir Rufo Oct 09 '15 at 16:29
  • RuleOfThumb: If you expect an instance may be disposed or `nil` just check before with an extension of the well known `if Assigned( foo ) {new->} and not foo.Disposed then // work with foo` – Sir Rufo Oct 09 '15 at 17:34
  • @SirRufo I wrote valid object. That means object you can actually use, not zombie object that is only left to keep reference counting mechanism in order. Delphi ARC implementation is a hybrid, it does not fully follow rules of pure ARC memory management. – Dalija Prasnikar Oct 09 '15 at 18:50
  • @SirRufo If you ever have the need to test `Disposed` property (besides in short term debugging code) then you have some really bad code. If you have pure ARC code then ARC `valid/nil` rule works, and if you have code that has to work under both compilers or interact with such code, if you touch object after it has been disposed then you are breaking manual memory management rules. – Dalija Prasnikar Oct 09 '15 at 18:56

0 Answers0