Recent

Author Topic: Understanding memory management in FPC/Lazarus - tl:dr  (Read 5625 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1013
    • HowTos Considered Harmful?
Understanding memory management in FPC/Lazarus - tl:dr
« on: February 06, 2013, 11:49:27 am »
I am trying to wrap my head around memory management in FPC/Lazarus and need to ask some questions about Free, Nil and Destroy and the side effects of TSomeObject.Create where there is no explicit Destroy(ing).

I take it that Free reclaims the space used by an object, and Nil sets the pointer that equates to the TSomeObject to zero (ensures that it doesn't point to anything). I also take it that when a procedure exits the memory allocated to the variables declared in it are it reclaimed, but items they were created to point to will hang around unless actively freed.

Does the Destroy procedure imply the existence of a Destructor procedure that is meant to Free, Nil and Destroy the objects create by the TSomeObject, TComponent or whatever?

Also are these scenarios involving TSomeObject.Create.

Code: [Select]
procedure TCreateObject.UseMemory;
var
  anObject: TSomeObject;
begin
  anObject := TSomeObject.Create;
end;
Memory will not be freed until application terminates?

Code: [Select]
procedure .TCreateObject.UseMemorySelf;
var
  anObject: TSomeObject;
begin
  anObject := TSomeObject.Create(self);
end;
Memory will not be freed until self object if destroyed?

Code: [Select]
procedure .TCreateObject.UseMemoryApplication;
var
  anObject: TSomeObject;
begin
  anObject := TSomeObject.Create(Application);
end;
Memory will not be freed until application terminates?

Code: [Select]
procedure .TComponentCreate.UseComponent;
var
  anObject: TComponent;
begin
  anObject := TComponent.Create(AOwner);
end;
Memory will not be freed until AOwner is destroyed.

Last bit.

Say in the main unit I have

Code: [Select]
var
  objectArray = array[1..10] of TSomeObject;
and throughout the program I do stuff like:

Code: [Select]
procedure TThisObject.CreateSomeObjects
var
   someObject: TSomeObject;
begin
  someObject := TSomeObject.Create;
  objectArray[next] := someObject;
  next := next+1;
end;
When that units ends I will have to so something like
Code: [Select]
for  i := 0 to TSomeObjectTypes.Count - 1 do
begin
  if Assigned(objectArray[i+1]) then
    objectArray[i+1].Destroy
end;
Right?

A lot of the stuff I do entails components placed on some forms, so I take it that when the forms are closed and destroyed, the memory they use will be freed, but that means that if any of the units and components they use create objects which are not destroyed by the components that create them they will lurk around till the application itself terminates?

Are there some things which are automatically freed, like those so labelled reference-counted objects like TAnsiStrings etc?

I am looking forward to interesting times indeed  8)
« Last Edit: February 07, 2013, 04:19:26 pm by vfclists »
Lazarus 3.0/FPC 3.2.2

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Understanding memory management in FPC/Lazarus - tl:dr
« Reply #1 on: February 06, 2013, 05:15:54 pm »
Quote
Does the Destroy procedure imply the existence of a Destructor procedure that is meant to Free, Nil and Destroy the objects create by the TObject, TComponent or whatever?
If I understand your question, yes.
Quote
Memory will not be freed until application terminates?
Yes
Quote
Memory will not be freed until self object if destroyed?
No, it will not even compile (TObject has no overloaded constructor accepting an argument)
Quote
Memory will not be freed until application terminates?
Same as above
Quote
Memory will not be freed until AOwner is destroyed.
Yes
Quote
When that units ends I will have to so something like ... Right?
Yes, assuming TObjectTypes.Count = Length(objectArray)
Quote
A lot of the stuff I do entails components placed on some forms, so I take it that when the forms are closed and destroyed, the memory they use will be freed, but that means that if any of the units and components they use create objects which are not destroyed by the components that create them they will lurk around till the application itself terminates?
Yes
Quote
Are there some things which are automatically freed, like those so labelled reference-counted objects like TAnsiStrings etc?
Yes, you already mentioned it, plus dynamic arrays, strings (all kinds) and objects implementing COM interface.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Understanding memory management in FPC/Lazarus - tl:dr
« Reply #2 on: February 06, 2013, 07:42:09 pm »
Quote
Are there some things which are automatically freed, like those so labelled reference-counted objects like TAnsiStrings etc?
Yes, you already mentioned it, plus dynamic arrays, strings (all kinds) and objects implementing COM interface.

"strings (all kinds)" - with the exception of shortstrings, of course.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Understanding memory management in FPC/Lazarus - tl:dr
« Reply #3 on: February 07, 2013, 01:51:05 pm »
Quote
"strings (all kinds)" - with the exception of shortstrings, of course.
It's too, despite the allocation happens on the stack (well, the question is about automatic memory freeing, regardless the memory region).

 

TinyPortal © 2005-2018