[fpc-pascal] fpc has trouble with array types

Sven Barth pascaldragon at googlemail.com
Tue Oct 18 11:36:15 CEST 2011


Am 18.10.2011 11:33, schrieb Sven Barth:
> Am 18.10.2011 11:19, schrieb Vincent Snijders:
>> 2011/10/18 Sven Barth<pascaldragon at googlemail.com>:
>>> Am 18.10.2011 11:12, schrieb Sven Barth:
>>>>
>>>> type
>>>> TByteArray = array of Byte;
>>>>
>>>> function XlatPrime(): TByteArray;
>>>> begin
>>>> XlatPrime := TByteArray.Create(
>>>> $64, $73, $66, $64, $3b, $6b, $66, $6f,
>>>> $41, $2c, $2e, $69, $79, $65, $77, $72,
>>>> $6b, $6c, $64, $4a, $4b, $44, $48, $53,
>>>> $55, $42, $73, $67, $76, $63, $61, $36,
>>>> $39, $38, $33, $34, $6e, $63, $78, $76,
>>>> $39, $38, $37, $33, $32, $35, $34, $6b,
>>>> $3b, $66, $67, $38, $37
>>>> );
>>>> end;
>>>
>>> Before I forget it: If you use this solution (not the const one) or
>>> you use
>>> "SetLength", you need to free the array using "SetLength(0)" or
>>> "YourArrayVariable := Nil", otherwise you'll have a memory leak. You
>>> must
>>> not do this if you use the const solution.
>>
>> That is unexpected. Until now, SetLength was used for ansistrings and
>> dynamic arrays, which didn't have this requirement (unless you messed
>> with the internals using Move).
>
> Could be that I missed the reference counting of arrays. At least I
> always try to "free" my arrays by hand ^^

Yes, seems like I missed the ref counting. See 
http://www.freepascal.org/docs-html/ref/refsu15.html#x39-430003.3.1

====
As remarked earlier, dynamic arrays are reference counted: if in one of 
the previous examples A goes out of scope and B does not, then the array 
is not yet disposed of: the reference count of A (and B) is decreased 
with 1. As soon as the reference count reaches zero the memory, 
allocated for the contents of the array, is disposed of.
====

Regards,
Sven



More information about the fpc-pascal mailing list