Recent

Author Topic: How to free memory after using dynamic array  (Read 6951 times)

Runner28

  • Newbie
  • Posts: 2
How to free memory after using dynamic array
« on: July 19, 2015, 02:00:37 pm »
I'm using an dynamic array, something like this:

type
   TListRec_part = record
                              Z1,Z2: string[10];
                           end;

   TListRec = record
                         X1: integer;
                         X2: string[20];
                         X3: array of TListRec_part;
                    end;
var
  List_complet: array of TListRec;


When is no need of this structures, I want to free memory.
I was try
       SetLength(List_complet,0);
or
     List_complet:=nil;
or
      SetLength(List_complet[1].X3,0);

but it gives me an error (Externel:SIGSEGV).
How I may free memory used by this structure?
Thx.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8774
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: How to free memory after using dynamic array
« Reply #1 on: July 19, 2015, 03:09:57 pm »
No need to to any of them to free memory used by dynamic arrays. It's reference counted and will automatically be freed when no longer referenced. Your SIGSEGV problem comes from somewhere else but it could be related to the memory management, but no one can know without you showing a small compilable program that demonstrates the SIGSEGV.

Handoko

  • Hero Member
  • *****
  • Posts: 5374
  • My goal: build my own game engine using Lazarus
Re: How to free memory after using dynamic array
« Reply #2 on: July 19, 2015, 04:36:00 pm »
What about TFPObjectList? Will it be removed automatically when the program closed or must I free it manually?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: How to free memory after using dynamic array
« Reply #3 on: July 19, 2015, 04:54:08 pm »
What about TFPObjectList? Will it be removed automatically when the program closed or must I free it manually?
To get you started think of it like this, There are no managed data types in pascal except strings, dynamic arrays and COM interfaces. Everything else needs your attention to get and release the memory needed.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to free memory after using dynamic array
« Reply #4 on: July 19, 2015, 04:59:04 pm »
In the particular case of TFPObjectList, If you instantiate your objectlist like this, then the objects will be freed by the list:
Code: [Select]
...
  myObjList:=TFPObjectList.Create(True);
  with myObjList do
    begin
      ...
    end;

Handoko

  • Hero Member
  • *****
  • Posts: 5374
  • My goal: build my own game engine using Lazarus
Re: How to free memory after using dynamic array
« Reply #5 on: July 19, 2015, 05:46:06 pm »
Okay, I understand now.

Items inside TFPObjectList will be freed automatically if I use:
Code: [Select]
myObjectList := TFPObjectList.Create(True);
But I have to remove them manually if I use:
Code: [Select]
myObjectList := TFPObjectList.Create(False);
I have to free the list itself (TFPObjectList) manually, no matter it's created with True or False parameter:
Code: [Select]
myObjectList.Free
Thanks all.

Runner28

  • Newbie
  • Posts: 2
Re: How to free memory after using dynamic array
« Reply #6 on: July 20, 2015, 03:19:50 pm »
Thx to Leledumbo:

I was wrong, that my problem is in freeing memory used by dyn. array.
After debugging I found source of my problem. As you wrote - my code has an error with memory management.
Thank you for reply and have a nice day.

 

TinyPortal © 2005-2018