* * *

Author Topic: [SOLVED] Freeing dynamic array for paranoid :)  (Read 1079 times)

Eugene Loza

  • Hero Member
  • *****
  • Posts: 508
    • My "almost daily" development blog
[SOLVED] Freeing dynamic array for paranoid :)
« on: April 20, 2017, 08:57:23 am »
I have several large dynamic 3D arrays as "array of array of array of integer;" or "of TSomeObject", etc. Not too much, but most of them are temporary. So after execution of main algorithm procedure I try to "free" them, as they are variables in a class, and therefore would be freed only after class destructor.
So I try to free them this way (just the reverse way they are created):
Code: Pascal  [Select]
  1.   for ix := 0 to length(nmap)-1 do begin
  2.     for iy := 0 to length(nmap[ix])-1 do
  3.       setLength(nmap[ix,iy],0);
  4.     setLength(nmap[ix],0);
  5.   end;
  6.   setLength(nmap,0);
Well... yes, that's paranoiac :)
So, I wonder, maybe just
Code: Pascal  [Select]
  1. setLength(nmap,0);
is enough to free all the memory of the dynamically allocated array? I make sure that all specific object references (if any) are freed before setting the length of the array to zero.
This matters significantly, as this procedure is "duplicated" in a destructor, which might (under some idiotic conditions) fire on half-freed or half-created array, so trying to access nmap[ix,iy] would lead to a error.
« Last Edit: April 20, 2017, 01:57:05 pm by Eugene Loza »
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

Wargan

  • New member
  • *
  • Posts: 44
    • 'This way' site
Re: Freeing dynamic array for paranoid :)
« Reply #1 on: April 20, 2017, 09:08:01 am »
You want to release them from memory?

What about the Finalize function?
« Last Edit: April 20, 2017, 09:09:32 am by Wargan »
Lazarus 1.6.4 stable + FPC 3.0.2.
Windows XP SP3, RAM 1 Gb - testing and commissioning programs for old computers.

jmm72

  • Jr. Member
  • **
  • Posts: 79
  • Very experienced in being a beginner...
Re: Freeing dynamic array for paranoid :)
« Reply #2 on: April 20, 2017, 10:26:41 am »
Initialize and Finalize are for variables allocated with GetMem. If the dynamic array was declared in a var block (in a function, or in the interface or implementation section) its heap memory is managed automatically.

As for the original question, they are variables in a class. Both code snippets should be equivalent, provided that beforehand they are traversed entirely to free the objects they contain. For other types like integers or strings, you can set the length of the 1st dimension of the array to 0 safely, the compiler will do everything for you. I'd say that both code snippets take approximately the same amount of time. I'm using dynamic arrays constantly and never had a memory leak due to them. Only with objects for now, when I mess up about when I am freeing them or not.
Lazarus 1.6.4 + FPC 3.0.2 64bits under Windows 7 64bits
Only as a hobby nowadays
Current proyect release: TBA

Eugene Loza

  • Hero Member
  • *****
  • Posts: 508
    • My "almost daily" development blog
Re: Freeing dynamic array for paranoid :)
« Reply #3 on: April 20, 2017, 11:23:10 am »
Wargan, jmm72 thanks a lot!
"set the length of the 1st dimension of the array to 0 safely" should do it.

P.S. I wasn't talking about memory leaks, but the situation is the following:

I generate a game map in a thread (let's those be 8 threads).
At some point threads peak their memory consumptions (when checking for map consistency).
Eventually they finish and stop waiting until other code (a generation manager) "takes" and "saves" the maps.
During this time more threads can start which will also require some memory.
So, just to minimize the memory use I can "free" some of the temporary arrays, so that RAM frees up for next processes.
e.g. the cycle looks like:

Generation class create
Init parameters
Start thread
Make temporary arrays (use a lot of RAM in case the map is big :))
Process the map
Free temporary arrays (free those megabytes)
Halt and wait for the data to be copied
External manager frees the generation class
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

ASerge

  • Sr. Member
  • ****
  • Posts: 453
Re: [SOLVED] Freeing dynamic array for paranoid :)
« Reply #4 on: April 20, 2017, 08:54:13 pm »
So, I wonder, maybe just
Code: Pascal  [Select]
  1. setLength(nmap,0);
is enough to free all the memory of the dynamically allocated array? I make sure that all specific object references (if any) are freed before setting the length of the array to zero.
nmap := nil enough.
If the array contains pointers to memory allocated manually, free them before.
Before using an array check it for nil.

Eugene Loza

  • Hero Member
  • *****
  • Posts: 508
    • My "almost daily" development blog
Re: [SOLVED] Freeing dynamic array for paranoid :)
« Reply #5 on: April 21, 2017, 05:58:50 pm »
nmap := nil enough.
Before using an array check it for nil.
FANTASTIC!!! The simplicity itself! THANKS!!!
Lazarus 1.9 + FPC 3.1.1 Debian Jessie 64 bit.

My Free and Open Source games in Lazarus/FreePascal/CastleGameEngine:
https://decoherence.itch.io/
(and some ancient games in Turbo Pascal too)
Sources are here: https://github.com/eugeneloza?tab=repositories

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus