Recent

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

Slyde

  • Full Member
  • ***
  • Posts: 152
[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 21.3
Lazarus 3.0

bytebites

  • Hero Member
  • *****
  • Posts: 639
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

  • Full Member
  • ***
  • Posts: 152
Re: [SOLVED] To Free, or not to Free?
« Reply #2 on: June 26, 2022, 08:57:46 am »
Thank you.
Linux Mint 21.3
Lazarus 3.0

dseligo

  • Hero Member
  • *****
  • Posts: 1219
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

  • Full Member
  • ***
  • Posts: 152
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 21.3
Lazarus 3.0

 

TinyPortal © 2005-2018