bg5
2015-04-04 04:21:41
So, I'm dealing with one function:
and I've noticed memory leak of the size of an Array. Yes, this function works properly, except it's leaking. After some testing I've found out that if you comment out this section leak is gone:
but I have no idea if it's original array or copy not being freed and also why this piece of code causes leaking.
Test script:
[SPOILER=Test script:]
[/SPOILER]
Please help ;)
procedure QuickSortArray (var AnyArray : Pointer; SizeOf_Index:integer; Pattern : TIntegerArray);
var
SoP,hArr,hP,i,j : integer;
tempArray : Pointer;
begin
Sop := sizeOf(Pointer);
hArr := integer((AnyArray-sop)^);
hP := High(Pattern);
if hArr <> hP then
begin
writeln('SortArray: Error: length of array is '+tostr(hArr+1)+' while length of pattern is '+tostr(hP+1));
exit;
end;
tempArray := AllocMem(SizeOf_Index*(hArr+1)); // temp copy of array
for i:=0 to hArr do
for j:=0 to SizeOf_Index-1 do
PtrByte(tempArray+(SizeOf_Index*i)+j)^ := PtrByte(AnyArray+(SizeOf_Index*i)+j)^;
for i:=0 to hArr do
for j:=0 to SizeOf_Index-1 do
PtrByte(AnyArray+(SizeOf_Index*i)+j)^ := PtrByte(tempArray+(SizeOf_Index*Pattern[i])+j)^;
writeln(SizeOf_Index*(hArr+1));
FreeMemSize(tempArray,SizeOf_Index*(hArr+1));
end; and I've noticed memory leak of the size of an Array. Yes, this function works properly, except it's leaking. After some testing I've found out that if you comment out this section leak is gone:
{ for i:=0 to hArr do
for j:=0 to SizeOf_Index-1 do
PtrByte(AnyArray+(SizeOf_Index*i)+j)^ := PtrByte(tempArray+(SizeOf_Index*Pattern[i])+j)^; }but I have no idea if it's original array or copy not being freed and also why this piece of code causes leaking.
Test script:
[SPOILER=Test script:]
program new;
type TCustomType = record
name:string;
TPA :TPointArray;
TSA : TStringArray;
ex:extended;
end;
type TCustomTypeArray = array of TcustomType;
procedure TCustomTypeArray.Create(size : integer);
var i,j :integer;
begin
SetLength(self,size);
for i:=0 to size-1 do
begin
self[i].name := tostr(i);
SetLength(self[i].TPA,i+1);
SetLength(self[i].TSA,i+1);
for j:=0 to i do
begin
self[i].TPA[j] := Point(i,j);
self[i].TSA[j] := 'MyName '+tostr(j);
end;
end;
// writeln(self);
end;
type PtrByte = ^byte;
procedure QuickSortArray (var AnyArray : Pointer; SizeOf_Index:integer; Pattern : TIntegerArray);
var
SoP,hArr,hP,i,j : integer;
tempArray : Pointer;
begin
Sop := sizeOf(Pointer);
hArr := integer((AnyArray-sop)^);
hP := High(Pattern);
if hArr <> hP then
begin
writeln('SortArray: Error: length of array is '+tostr(hArr+1)+' while length of pattern is '+tostr(hP+1));
exit;
end;
tempArray := AllocMem(SizeOf_Index*(hArr+1));
for i:=0 to hArr do
for j:=0 to SizeOf_Index-1 do
PtrByte(tempArray+(SizeOf_Index*i)+j)^ := PtrByte(AnyArray+(SizeOf_Index*i)+j)^;
for i:=0 to hArr do
for j:=0 to SizeOf_Index-1 do
PtrByte(AnyArray+(SizeOf_Index*i)+j)^ := PtrByte(tempArray+(SizeOf_Index*Pattern[i])+j)^;
writeln(SizeOf_Index*(hArr+1));
FreeMemSize(tempArray,SizeOf_Index*(hArr+1));
end;
var a,b: TCustomTypeArray;
TIA :TIntegerarray;
begin
a.Create(300);
setLength(TIA,300);
QuickSortArray(Pointer(a),sizeof(a[0]),TIA);
{
b.Create(4);
writeln(b);
QuickSortArray(Pointer(b),sizeof(b[0]),[3,1,2,0]);
writeln(b);
}
end.[/SPOILER]
Please help ;)