2

for flexibility different frames (like visible "modules" which can be build together by user - dashboard) shall be created and destroyed on a firemonkey form (some of them twice or more)

destroying a control or frame because of ressources had been easy in vcl:

frm:=   TMyFrame1.Create( Panel1);
frm.Parent:=    Panel1;
...
frm.Free;

but how to do (force) this in firemonkey ? (the "Free" only results in a "object.release" and didn't destroy the control because there seems to be other references - but the variable holding one frame is only used in a "create" and a "free")

Ken White
  • 123,280
  • 14
  • 225
  • 444
user2807653
  • 79
  • 2
  • 7

1 Answers1

2

"Free" should work in Windows / OS X. You'll have to use "DisposeOf" in iOS / Android because of ARC.

Giel
  • 2,066
  • 20
  • 22
  • But what about the other references that are keeping the object alive? Will they recognise the zombie? – David Heffernan Nov 16 '13 at 10:05
  • TObject.Disposed can be used to detect if an object got disposed of. But you don't want to go there if you can avoid it. Note that any references related to TComponent internals will clean up automatically if you dispose a component. – Giel Dec 08 '13 at 19:22
  • Yes, `Disposed` can be called. But will it? That was the question I asked. – David Heffernan Dec 08 '13 at 19:24
  • AFAIK the RTL doesn't call it, it doesn't need to since a disposed object can safely be nilled. – Giel Dec 08 '13 at 19:30
  • You are not understanding my point. What happens when the RTL calls a method on an instance that has been disposed? – David Heffernan Dec 08 '13 at 19:31
  • It will probably crash..... – Giel Dec 08 '13 at 19:34
  • So, take a look at what this means to your answer – David Heffernan Dec 08 '13 at 19:49
  • No, you should actually try using ARC yourself instead. Like I said, this all works fine with TComponents (and controls etc.). There's no magic secret reference accessing the disposed TMyFrame1. – Giel Dec 08 '13 at 19:58
  • Oh. So what did you mean by "it will probably crash". Was that a mistake? – David Heffernan Dec 08 '13 at 19:59
  • You asked what would happen if a method on a disposed instance gets called. But that doesn't happen (unless you do it yourself of course). "DisposeOf" means "I want to call the destructor now to free resources". Closing files is a common reason to call DisposeOf. TComponents (and descendants) are a bit special: you need to call DisposeOf because the inherent parent/owner references prevent "Free" from destroying the object. It basically does the same thing "Free" used to do: the component gets destroyed and everyone who's interested is notified. – Giel Dec 08 '13 at 20:10
  • If you call `DisposeOf`, then you've got a zombie. Call it on an FMX GUI object and what makes you believe that the FMX framework will decide to stop calling methods on that object? I see no reason for that to happen. – David Heffernan Dec 08 '13 at 20:18
  • If you call Free on a VCL control you have a dangling pointer. It doesn't crash because the TComponents notify each other and the destroying one is removed from all lists etc. This still works with ARC. – Giel Dec 08 '13 at 20:24