Recent

Author Topic: Memory issue?  (Read 625 times)

miol

  • Newbie
  • Posts: 1
Memory issue?
« on: December 20, 2025, 01:00:40 pm »
Hi, I'm new to the forum and to FPC programming. Have used C# years ago.
Running the following code works fine the first time. The second time I get:

Code: Text  [Select][+][-]
  1. Project Buildings raised exception class 'External: SIGSEGV'.
  2.  In file 'control.inc' at line 3620:
  3. len := GetTextLen;
  4.  

I assume this is a memory / Freeing problem? Please help..

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.lbBuildingsDblClick(Sender: TObject);
  2. var
  3.   selectedBuilding: TBuilding;
  4. begin
  5.   selectedBuilding := TBuilding(lbBuildings.Items.Objects[lbBuildings.ItemIndex]);
  6.  
  7.   if not Assigned(editForm) then
  8.     editForm := TFormBuilding.Create(Self);
  9.  
  10.   editForm.Building := selectedBuilding;
  11.   editForm.ShowModal;
  12.  
  13.   //building has been edited, replace the list item string, leaving the object untouched
  14.   lbBuildings.Items[lbBuildings.ItemIndex] := selectedBuilding.Name;
  15.  
  16. end;
  17.  

The error occurs when the line " editForm.Building := selectedBuilding;" is executed.

I've discovered that if I add a
  FreeAndNil(editForm);
after  ShowModal returns, it works as I was expecting, no error.

But I thought that one was able to reuse forms?
Any ideas?
Thank you!
« Last Edit: December 20, 2025, 01:38:34 pm by miol »

jamie

  • Hero Member
  • *****
  • Posts: 7527
Re: Memory issue?
« Reply #1 on: December 20, 2025, 07:03:26 pm »
My guess is you have something left over that isn't cleared from the previous code cycle in the TFORMBuilding.

don't know if you are using a Property Setter for the "SelectedBuilding" but if you are, maybe you are using old invalid data?

Normally when creating a new object and TFORMBuilding could be considered a new object, it clears all the data space so any variables or whatever you had set there is gone and a clean slate is started.

 I would use the OnShow or ever maybe of the form and clear it all before setting anything. Also, use HeapTrc in the debugging to ensure you have no memory hanging around.

Jamie


The only true wisdom is knowing you know nothing

backprop

  • Full Member
  • ***
  • Posts: 158
Re: Memory issue?
« Reply #2 on: December 20, 2025, 08:57:24 pm »
I've discovered that if I add a
  FreeAndNil(editForm);
after  ShowModal returns, it works as I was expecting, no error.

But I thought that one was able to reuse forms?
Any ideas?

When dynamically create forms,  It is not good practice to assume it is nil during application installation.

In MainFormCreate() event insert editForm :=nil and that should solve the problem.

But better to add minimal project source which represent the problem.


marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12645
  • FPC developer.
Re: Memory issue?
« Reply #3 on: December 21, 2025, 11:08:41 am »
You also give the parent form as owner.   If the main form respawns, then editform is a stale reference

Code: Pascal  [Select][+][-]
  1.  editForm := TFormBuilding.Create(Self);

 

TinyPortal © 2005-2018