Delphi / FireMonkey release components created at runtime [Edit]

Hello,
XE6, Win7
Am I right I thinking that if I create a component, such as TImage, at runtime, Delphi automatically releases the memory when the program closes.
I declared this:
  im: TImage;
as a TForm variable.
Put this in .FormCreate:
  ReportMemoryLeaksOnShutdown := DebugHook <> 0;
This picks up things like TStrokeBrush when I don't run .Free;
I then put this in:
procedure TFrmScales.Button4Click(Sender: TObject);
begin
  im := TImage.Create(self);
  im.Bitmap.Assign(I_note.Bitmap);  // I_note is a TImage that I put there at design-time
  im.Parent := self;
  im.Position.X := 10;
  im.Position.Y := 10;
end;
If I don't run im.Free there is no memory leak reported.
So, it looks like Delphi cleans up visual components that are created at runtime - is this correct?
Many thanks,
Ian
Edited by: Ian Johnson on Jul 14, 2014 8:58 PM
0
Ian
7/15/2014 4:06:17 AM
📁 embarcadero.delphi.firemonkey
📃 4901 articles.
⭐ 4 followers.

💬 3 Replies
👁️‍🗨️ 2251 Views


Ian Johnson wrote:
> Hello,
> 
> XE6, Win7
> 
> Am I right I thinking that if I create a component, such as TImage, at runtime, Delphi automatically releases the memory when the program closes.
Under the mobile compilers, yes since the ref count will go to 0 and the memory 
released by ARC.  In desktop no, but the OS will claim all memory when an app 
closes so it is not really a big deal.  Still good practice to cleanup after 
yourself, but sometimes if the cleanup takes time you can just let the OS 
reclaim the whole heap blocks.
> 
> I declared this:
> 
>   im: TImage;
> 
> as a TForm variable.
> 
> Put this in .FormCreate:
> 
>   ReportMemoryLeaksOnShutdown := DebugHook <> 0;
> 
> This picks up things like TStrokeBrush when I don't run .Free;
> 
> I then put this in:
> 
> procedure TFrmScales.Button4Click(Sender: TObject);
> begin
>   im := TImage.Create(self);
>   im.Bitmap.Assign(I_note.Bitmap);  // I_note is a TImage that I put there at design-time
>   im.Parent := self;
>   im.Position.X := 10;
>   im.Position.Y := 10;
> end;
> 
> If I don't run im.Free there is no memory leak reported.
> 
> So, it looks like Delphi cleans up visual components that are created at runtime - is this correct?
> 
> Many thanks,
> 
> Ian
> 
> Edited by: Ian Johnson on Jul 14, 2014 8:58 PM
In your example code the component is owned by the form so when the form is 
destroyed it frees all things it owns.  The TImage will also free all its 
resources when the form frees it.
In this case mobile and desktop should behave essentially the same way as the 
owner will be responsible for freeing/niling the references to the object 
resulting in the memory being released.
-- 
Jeff Overcash (TeamB)
       (Please do not email me directly unless  asked. Thank You)
And so I patrol in the valley of the shadow of the tricolor
I must fear evil. For I am but mortal and mortals can only die.
Asking questions, pleading answers from the nameless
faceless watchers that stalk the carpeted  corridors of Whitehall.
              (Fish)
0
Jeff
7/15/2014 4:36:32 AM
> {quote:title=Jeff Overcash (TeamB) wrote:}{quote}
> Ian Johnson wrote:
> > Hello,
> > 
> > XE6, Win7
> > 
> > Am I right I thinking that if I create a component, such as TImage, at runtime, Delphi automatically releases the memory when the program closes.
> 
> Under the mobile compilers, yes since the ref count will go to 0 and the memory 
> released by ARC.  In desktop no, but the OS will claim all memory when an app 
> closes so it is not really a big deal.  Still good practice to cleanup after 
> yourself, but sometimes if the cleanup takes time you can just let the OS 
> reclaim the whole heap blocks.
> 
> > 
> > I declared this:
> > 
> >   im: TImage;
> > 
> > as a TForm variable.
> > 
> > Put this in .FormCreate:
> > 
> >   ReportMemoryLeaksOnShutdown := DebugHook <> 0;
> > 
> > This picks up things like TStrokeBrush when I don't run .Free;
> > 
> > I then put this in:
> > 
> > procedure TFrmScales.Button4Click(Sender: TObject);
> > begin
> >   im := TImage.Create(self);
> >   im.Bitmap.Assign(I_note.Bitmap);  // I_note is a TImage that I put there at design-time
> >   im.Parent := self;
> >   im.Position.X := 10;
> >   im.Position.Y := 10;
> > end;
> > 
> > If I don't run im.Free there is no memory leak reported.
> > 
> > So, it looks like Delphi cleans up visual components that are created at runtime - is this correct?
> > 
> > Many thanks,
> > 
> > Ian
> > 
> > Edited by: Ian Johnson on Jul 14, 2014 8:58 PM
> 
> In your example code the component is owned by the form so when the form is 
> destroyed it frees all things it owns.  The TImage will also free all its 
> resources when the form frees it.
> 
> In this case mobile and desktop should behave essentially the same way as the 
> owner will be responsible for freeing/niling the references to the object 
> resulting in the memory being released.
> 
> -- 
> Jeff Overcash (TeamB)
>        (Please do not email me directly unless  asked. Thank You)
> And so I patrol in the valley of the shadow of the tricolor
> I must fear evil. For I am but mortal and mortals can only die.
> Asking questions, pleading answers from the nameless
> faceless watchers that stalk the carpeted  corridors of Whitehall.
>               (Fish)
Many thanks Jeff
0
Ian
7/15/2014 5:04:16 AM
> {quote:title=Jeff Overcash (TeamB) wrote:}{quote}
> Ian Johnson wrote:
> > Hello,
> > 
> > XE6, Win7
> > 
> > Am I right I thinking that if I create a component, such as TImage, at runtime, Delphi automatically releases the memory when the program closes.
> 
> Under the mobile compilers, yes since the ref count will go to 0 and the memory 
> released by ARC.  In desktop no, but the OS will claim all memory when an app 
> closes so it is not really a big deal.  Still good practice to cleanup after 
> yourself, but sometimes if the cleanup takes time you can just let the OS 
> reclaim the whole heap blocks.
> 
> > 
> > I declared this:
> > 
> >   im: TImage;
> > 
> > as a TForm variable.
> > 
> > Put this in .FormCreate:
> > 
> >   ReportMemoryLeaksOnShutdown := DebugHook <> 0;
> > 
> > This picks up things like TStrokeBrush when I don't run .Free;
> > 
> > I then put this in:
> > 
> > procedure TFrmScales.Button4Click(Sender: TObject);
> > begin
> >   im := TImage.Create(self);
> >   im.Bitmap.Assign(I_note.Bitmap);  // I_note is a TImage that I put there at design-time
> >   im.Parent := self;
> >   im.Position.X := 10;
> >   im.Position.Y := 10;
> > end;
> > 
> > If I don't run im.Free there is no memory leak reported.
> > 
> > So, it looks like Delphi cleans up visual components that are created at runtime - is this correct?
> > 
> > Many thanks,
> > 
> > Ian
> > 
> > Edited by: Ian Johnson on Jul 14, 2014 8:58 PM
> 
> In your example code the component is owned by the form so when the form is 
> destroyed it frees all things it owns.  The TImage will also free all its 
> resources when the form frees it.
> 
> In this case mobile and desktop should behave essentially the same way as the 
> owner will be responsible for freeing/niling the references to the object 
> resulting in the memory being released.
> 
> -- 
> Jeff Overcash (TeamB)
>        (Please do not email me directly unless  asked. Thank You)
> And so I patrol in the valley of the shadow of the tricolor
> I must fear evil. For I am but mortal and mortals can only die.
> Asking questions, pleading answers from the nameless
> faceless watchers that stalk the carpeted  corridors of Whitehall.
>               (Fish)
Hi Jeff - just posted a message to say thanks, and then thought of another related point! 
As far as I can work out, the only way I can close an application running on the iOS simulator is to terminate it from Delphi.  Even if I close it in the simulator, Delphi doesn't seem to know.  This means that I can't check for memory leaks as it hasn't closed properly.  That's why I'm testing it under Win32 but if the rules are different that could be tricky.  Is there a way of checking for leaks when it's running under the simulator?
Thanks, Ian
0
Ian
7/15/2014 5:16:08 AM
Reply: