Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #11  
Old 09-23-2008, 11:22 PM
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 guys,

Thank you all for your answers. I have tried the method presented in http://delphi.about.com/od/kbcurt/a/runtime_array.htm but the memory is not freed...

I have also tried Chris sample code(Thanks a lot mate) but I get a compilation error Invalid Typecast.

I have also tried this but nothing
[DELPHI]
for c:= numofslices*2-1 downto 0 do
begin
t:= contours[c].Count -1;
for cc:= t downto 0 do
begin
Dispose(Contours[c].Items[cc]);
Contours[c].Delete(cc);
end;
end;
[/DELPHI]
Reply With Quote
  #12  
Old 09-23-2008, 11:45 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

Hello there! Last week I also create a dynamic Tlist for my client. I've encountered that problem as well. By the way, what delphi version did you use? Because it matters here and we can find the solution.

Take a look in my sample creating a runtime dynamic TList:
...
procedure freelist(arylst:array of TListBox);
private
MyLstBx : array of TListBox; //it should be declared here, so that i can access it to another method
pulic
...


//this will free my array list with no errors
procedure TForm1.freelist(arylst:array of TListBox);
var
i : Integer;
begin
for i := Low(arylst) to High(arylst) do
begin
arylst[i].Free;
arylst[i]:=nil;
end;
end;

//this will create my TList. I've modified it to
procedure TForm1.Button1Click(Sender: TObject);
var
i, j, a, b, set_mytop : Integer;
nextline : Boolean;
begin
SetLength(MyLstBx, StrToInt(Edit1.Text));
nextline := False;
j := StrToInt(Edit2.Text);
set_mytop := 80;
a:=0; b:=1;

for i := 0 to StrToInt(Edit1.Text) - 1 do
begin
a := a + 1;
MyLstBx[i] := TListBox.Create(Self);
MyLstBx[i].Name := 'mylistbox' + IntToStr(i);

if i = j then //dont mind this condition, it is just a position where to place my tlist
begin
a := 1;
b := b + 1;
j := j + StrToInt(Edit2.Text);
nextline := true;
end;

MyLstBx[i].Left := (30 * a) + (200 * (a-1));

if nextline then
begin
set_mytop := 50 + (30 * b) + (200 * (b-1));
nextline := False;
end;

MyLstBx[i].Top := set_mytop;

MyLstBx[i].BorderStyle :=bsNone;
MyLstBx[i].Width := 200;
MyLstBx[i].Height := 200;
MyLstBx[i].Color := clWhite;
MyLstBx[i].Visible :=True;
MyLstBx[i].Parent := Self;
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
freelist(MyLstBx); //assign my "MyLstBx" to a freelist, so that it will clear and free the TList.
end;



Enjoy!
Reply With Quote
  #13  
Old 09-24-2008, 12:03 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

I am using Delphi 2005.

Cheers
Reply With Quote
  #14  
Old 09-24-2008, 03:03 AM
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 would work with the High and Low, but that asside. You never make the contours array any smaller, so it'll keep reserving the memory...

In my mind I thought of another prolem I once had with dynamic arrays. Setting the length to something smaller would not reside in memory becoming smaller. The memory is still reserved, but re-used at the point you're increasing the dynamic array again... Not sure which version of Delphi this was, but in one version it was there...

But nowadays, if you don't trust the memory being free'ed, you can use ReportMemoryLeadsOnShutDown (D2007 has it for sure). If I'm correct it's from FastMM and it'll report you on memory leaks. Just set it True in the .dpr and see if you have any memory leaks...

Objective reality is a delirium caused by lack of alcohol in blood.
There is no place like 127.0.0.1
Reply With Quote
  #15  
Old 09-24-2008, 05:50 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]
>>
I have also tried Chris sample code(Thanks a lot mate) but I get a compilation
error Invalid Typecast.
<<

Would need slightly more information than that. For instance what is a
TVector3f? Is it an object, record, mammal, food...? Where is the error?
[/pre]
[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
  #16  
Old 09-24-2008, 11:17 PM
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

TVector3f is declared in unit VectorTypes as array [0..2] of Single

Cheers
Reply With Quote
  #17  
Old 09-25-2008, 02:44 AM
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]
That's a little bit differant then...

[/pre][DELPHI]
type
PVector3f = ^TVector3f;
TVector3f = array[0..2]of single;

procedure TForm1.Button1Click(Sender: TObject);
var
Contours : array of TList;
i, j : integer;
Pv3 : PVector3f;
begin
//size array
SetLength(Contours, 32);
//create TLists
for i := Low(Contours) to High(Contours) do
Contours[i] := TList.Create;

//Populate TLists with some TVector3fs
for i := Low(Contours) to High(Contours) do begin
for j := 0 to 4 do begin
//Allocate a new TVector3f
New(Pv3);
Pv3^[0] := GetTickCount();
Pv3^[1] := GetTickCount();
Pv3^[2] := GetTickCount();
//Store it
Contours[i].Add(Pv3);
end;
end;

//Show one of them
showmessage( FloatToStr( PVector3f(Contours[3][4])^[1] ) );

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

Contours[i].Free;
end;

Contours[i] := nil;
end;

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

[/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
  #18  
Old 09-25-2008, 03:38 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

Yes, I have tried the dispose method you propose.Once again thanks a lot but the memory is not returned but to Windows. I believe the problem has to do with the fact the my contours array is defined as a global array in the global var section and not locally within a procedure. But the setlength and tlist.create loop are called from a tform.button. Another button.click procedure is used to do the supposed clean up.

Cheers
Reply With Quote
  #19  
Old 09-26-2008, 02:31 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]
Tested in D7, with Windows Task Manager open watching the memory
usage, the memory is being freed (when Button2 is clicked). Are
you sure it is not something else?

[/pre][DELPHI]
type
PVector3f = ^TVector3f;
TVector3f = array[0..2]of single;

var
Contours : array of TList;

procedure TForm1.Button1Click(Sender: TObject);
var
n : word;
i, j : integer;
Pv3 : PVector3f;
begin
n := 1000;
//size array
SetLength(Contours, n);

if n > 0 then begin
Button1.Enabled := false;

//create TLists
for i := Low(Contours) to High(Contours) do
Contours[i] := TList.Create;

//Populate TLists with some TVector3fs
for i := Low(Contours) to High(Contours) do begin
for j := 0 to n do begin
//Allocate a new TVector3f
New(Pv3);
Pv3^[0] := GetTickCount();
Pv3^[1] := GetTickCount();
Pv3^[2] := GetTickCount();
//Store it
Contours[i].Add(Pv3);
end;
end;

//Show one of them
showmessage( FloatToStr( PVector3f(Contours[Random(n)][Random(n)])^[1] ) );
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
i, j : integer;
begin
if High(Contours) > -1 then begin
//Free the TLists...
for i := Low(Contours) to High(Contours) do begin
if Assigned(Contours[i]) then begin
for j := 0 to Contours[i].Count -1 do
Dispose( PVector3f(Contours[i][j]) );

Contours[i].Free;
end;

Contours[i] := nil;
end;

//zero Contours...
SetLength(Contours, 0);

end;

Button1.Enabled := true;
end;


[/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
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 04:36 AM.


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