Lazarus

Programming => General => Topic started by: Slyde on June 26, 2022, 05:22:31 am

Title: [SOLVED] To Free, or not to Free?
Post by: Slyde on June 26, 2022, 05:22:31 am
Does TForm Free sm when sm's Ok button's clicked?

Code: Pascal  [Select][+][-]
  1. var
  2.   sm: TForm;
  3. begin
  4.   sm:= CreateMessageDialog('Well done', mtCustom, [mbOK]);
  5.   sm.Position:= poOwnerFormCenter;
  6.   sm.ShowModal;
  7. end;

Or do I need to do it?

Code: Pascal  [Select][+][-]
  1. var
  2.   sm: TForm;
  3. begin
  4.   sm:= CreateMessageDialog('Well done', mtCustom, [mbOK]);
  5.   sm.Position:= poOwnerFormCenter;
  6.   sm.ShowModal;
  7.   sm.Free;
  8. end;

Not a hassle one way or the other.  But since the both blocks of code run, I was wondering if the top block's giving a memory leak.
Title: Re: To Free, or not to Free?
Post by: bytebites on June 26, 2022, 07:56:41 am
Correct answer is Project options -> Debugging -> Use Heap trace -unit
Title: Re: [SOLVED] To Free, or not to Free?
Post by: Slyde on June 26, 2022, 08:57:46 am
Thank you.
Title: Re: [SOLVED] To Free, or not to Free?
Post by: dseligo on June 26, 2022, 10:24:39 am
IMHO, you have to free it.

There is no owner set in creation of form, so it isn't freed automatically:
Code: Pascal  [Select][+][-]
  1. inherited CreateNew(nil, 1);
Title: Re: [SOLVED] To Free, or not to Free?
Post by: Slyde on June 26, 2022, 04:54:21 pm
Hey, dseligo.

That's what I've done, just to be on the safe side.  I actually got that code from the forum (https://forum.lazarus.freepascal.org/index.php/topic,26852.msg165545.html#msg165545), here. And it was given without Freeing the dialog box, which no one said anything to the contrary.  But it just didn't look right.  And I didn't know where to start digging to sort it out on my my own, so I used the forum.  Really appreciate your answer.  Thanks.
TinyPortal © 2005-2018