Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-23-2008, 09:35 AM
Tsaknak Tsaknak is offline
Junior Member
 
Join Date: Sep 2008
Posts: 8
Default How to free memory of an array of Tlists - Out of Memory error

Hi all,

I was wondering if someone could give me advice on the following problem.

I am using a dynamic array of Tlists in order to hold some data. Say the name of the array contour.

I inialise the array with
SetLength(contour,32);
then I do a loop to create the lists
Contour[c] := Tlist.Create;
..then I do some other stuff and load the lists with data...

After that I want to delete all the data from the tlists and restart again. My problem is that no matter what I have tried the lists are cleared but the memory doesn't return to the system so after several recursions of the algorithm the system replies inevitably with an out of memory message.

What is the appropriate way to empty those lists in order for the executable to return the memory back to the system?

Cheers,
Tsaknak
Reply With Quote
  #2  
Old 09-23-2008, 09:59 AM
tomu tomu is offline
Senior Member
 
Join Date: May 2002
Posts: 186
Default RE: How to free memory of an array of Tlists - Out of Memory error

Befor freeing Contour you need to manually free everything that was created somewhere else in your app. For example, if Contour held dynamically creatd forms this should work:

procedure TForm1.Button1Click(Sender: TObject);
var
Contour : Tlist;
i : integer;
begin
for i := 0 to Contour.Count -1 do
begin
TForm(Contour[i]).free;
end;
end;

You may need to cast the items in Contour to something else:
TStringList(Contour[i]).free;
Reply With Quote
  #3  
Old 09-23-2008, 10:18 AM
Tsaknak Tsaknak is offline
Junior Member
 
Join Date: Sep 2008
Posts: 8
Default RE: How to free memory of an array of Tlists - Out of Memory error

Hi tomu,

Thank you for your reply. But I am a bit confused
So, I will just rephrase my question and maybe you can still help me with this.

Contours : Array of Tlist;
...
SetLength(Contours,32);
for c:= 0 to 32 do contours[c] := Tlist.Create;
Then add into in each list data of type TVector3f.
So, probably I have to clear the lists according to your answer in a way like the following:

for c:= 0 to 32 do
begin
TVector3f(Contours[c].items[cc]).free;
end;

I think the above doesn't compile but I didn't get how to perform the casting.

Cheers,
Tsaknak
Reply With Quote
  #4  
Old 09-23-2008, 10:32 AM
tomu tomu is offline
Senior Member
 
Join Date: May 2002
Posts: 186
Default RE: How to free memory of an array of Tlists - Out of Memory error

Maybe something like this. It might be more readable to declare a variable of type TVector3f (and any other types involved) and use multiple lines of code, instead of doing everything in a single line of code.

procedure TForm1.Button1Click(Sender: TObject);
var
Contour : Tlist;
i,j : integer;
OneStringList : Tstringlist;
begin
for i := 0 to Contour.Count -1 do
begin
OneStringList := Tstringlist(Contour[i]);
for j := 0 to OneStringList.Count - 1 do
TForm(OneStringList[j]).Free;
OneStringList.Free;
end;
end;
Reply With Quote
  #5  
Old 09-23-2008, 10:44 AM
Tsaknak Tsaknak is offline
Junior Member
 
Join Date: Sep 2008
Posts: 8
Default RE: How to free memory of an array of Tlists - Out of Memory error

Tomu thanks a lot mate for your prompt response. This is what I have came up with and get an access violation

procedure TForm1.Button32Click(Sender: TObject);
var c,cc: integer;
alist : Tlist;
begin
alist := Tlist.create;
for c:= 0 to numofslices*2-1 do
begin
alist := Contours[c]; // <- I get an access violation here
for cc := 0 to alist.count -1 do
Tform(alist[cc]).free;
alist.free;
end;
end;

hmmmm....
Reply With Quote
  #6  
Old 09-23-2008, 10:49 AM
tomu tomu is offline
Senior Member
 
Join Date: May 2002
Posts: 186
Default RE: How to free memory of an array of Tlists - Out of Memory error

I think you just need to cast what is in Contours:
alist := Tlist(Contours[c]);

procedure TForm1.Button32Click(Sender: TObject); var c,cc: integer;
alist : Tlist;
begin
alist := Tlist.create;
for c:= 0 to numofslices*2-1 do
begin
alist := Tlist(Contours[c]);
for cc := 0 to alist.count -1 do
Tform(alist[cc]).free;
alist.free;
end;
end;
Reply With Quote
  #7  
Old 09-23-2008, 11:18 AM
Tsaknak Tsaknak is offline
Junior Member
 
Join Date: Sep 2008
Posts: 8
Default RE: How to free memory of an array of Tlists - Out of Memory error

Thanks for the help mate but I really cannot make it work.
I either get a violation error...

Cheers,
Anestis
Reply With Quote
  #8  
Old 09-23-2008, 02:19 PM
chris_w chris_w is offline
Senior Member
 
Join Date: Jan 2004
Posts: 1,397
Default RE: How to free memory of an array of Tlists - Out of Memory error

[pre]
This might be easier to follow...

[/pre][DELPHI]
var
Contours : array of TList;

//populate Contours with 32 TLists...
SetLength(Contours, 32);
for i := 0 to 31 do
Countours[i] := TList.Create;

//or like this, which is more flexable:

SetLength(Contours, 32);
for i := Low(Contours) to High(Contours) do
Countours[i] := TList.Create;


//to free the lists...
// Assuming that you have added some TVector3fs to them
// and that TVector3fs need to be freed.

for i := Low(Contours) to High(Contours) do begin
if Assigned(Contours[i]) then begin
for j := 0 to Contours[i].Count -1 do
TVector3f( Contours[i][j] ).Free;

Contours[i].Free;
end;

Contours[i] := nil;
end;

//if you want to zero Contours...
// SetLength(Contours, 0);


[/DELPHI][pre]
"There is a theory which states that if ever anybody discovers
exactly what the Universe is for and why it is here, it will
instantly disappear and be replaced by something even more
bizarre and inexplicable. There is another theory which states
that this has already happened."
-- Douglas Adams
[/pre]

Chris
Reply With Quote
  #9  
Old 09-23-2008, 03:39 PM
irvinedelphi irvinedelphi is offline
Senior Member
 
Join Date: Apr 2006
Posts: 127
Default RE: How to free memory of an array of Tlists - Out of Memory error

Just go here for proper freeing an array or more tutorial/samples:

http://delphi.about.com/od/kbcurt/a/runtime_array.htm

Enjoy!
Reply With Quote
  #10  
Old 09-23-2008, 10:26 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,333
Default RE: How to free memory of an array of Tlists - Out of Memory error

I think he doesn't release the items in the TList... He free's the TList, but what happens with the items???
If he would've used a TObjectList then you could tell to release on free (OwnsObjects = True, default)...

Objective reality is a delirium caused by lack of alcohol in blood.
There is no place like 127.0.0.1
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 09:32 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.