Alexandre Leclerc wrote:
2006/6/7, Alexandre Leclerc <[EMAIL PROTECTED]>:
I asked the question for a string array. I see that we can assign a
string directly to a pchar, but when I SetLength() such an array, do I
have to free the PChar string no more used or is it done
automatically?
var
a: array of PChar;
begin
SetLength(a, 4);
a[0] := 'First pchar line';
a[1] := 'Second pchar line';
a[2] := 'Third pchar line';
a[3] := 'Fourth pchar line';
SetLength(a,2);
a[0] := 'Yet another pchar line';
a[1] := 'At last, a pchar line to finish';
end;
Is the following causing a memory leak? Do I have to SetLength(a, 0)
when I'm completely done with 'a' or is it done automatically?
This should not couse a memory leak.
The 6 "strings" you are using are const (and compiled into the binary)
Assigning them to an array of pchar, only stores a ponter to the text in
the array. There is no memory allocated for those strings, so they can't
leak.
When a goes out of scope, its memory gets freed, so you don't have to
set its length to 0
The following test shows a memory increase for the process each time
it is executed. So I guess that I have a memory leak. An array a PChar
is not a big deal then.
I think, this is not really a memory leak, but more likely memory
fragmentation, since you are allocating random lengths. However I don't
know if SetLength reduces the mem when the length of an array is
shortened. It might be that you are working with just one array of
length 1023
Marc
Is there a peculiar reason to use such an array for the DecodeCSVStr
function? (Suggested by Michael, but I send the question to all those
who are interested.)
procedure TMainForm.Button1Click(Sender: TObject);
var
a: array of PChar;
i: Integer;
j: Integer;
begin
Randomize;
for i := 0 to 10000 do
begin
SetLength(a, Random(1024));
for j := 0 to High(a) do
a[j] := 'this is a nice phrase to test teh pchar stuff, yeah,
the phrase is of fixed length, but I''ve no time to waste on this.';
end;
end;
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives