Recent

Author Topic: [SOLVED] To Free, or not to Free?  (Read 418 times)

Slyde

  • Jr. Member
  • **
  • Posts: 83
[SOLVED] To Free, or not to Free?
« 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.
« Last Edit: June 26, 2022, 08:08:44 am by Slyde »
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

bytebites

  • Sr. Member
  • ****
  • Posts: 493
Re: To Free, or not to Free?
« Reply #1 on: June 26, 2022, 07:56:41 am »
Correct answer is Project options -> Debugging -> Use Heap trace -unit

Slyde

  • Jr. Member
  • **
  • Posts: 83
Re: [SOLVED] To Free, or not to Free?
« Reply #2 on: June 26, 2022, 08:57:46 am »
Thank you.
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

dseligo

  • Hero Member
  • *****
  • Posts: 649
Re: [SOLVED] To Free, or not to Free?
« Reply #3 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);
« Last Edit: June 26, 2022, 10:26:47 am by dseligo »

Slyde

  • Jr. Member
  • **
  • Posts: 83
Re: [SOLVED] To Free, or not to Free?
« Reply #4 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, 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.
« Last Edit: June 26, 2022, 08:49:14 pm by Slyde »
Linux Mint 20.3 / Lazarus 2.2.2 / FPC 3.2.2

 

TinyPortal © 2005-2018