[fpc-pascal] reference-counted function results not initialized to nil

David Emerson dle3ab at angelbase.com
Sat Jun 25 08:31:06 CEST 2016

On 06/24/2016 08:19 PM, Michalis Kamburelis wrote:
>> After upgrading fpc 2.6.4 -> 3.0.0, I'm seeing a bug where (as noted in
>> subject) reference-counted function results are not being initialized to
>> nil.
> They were never guaranteed to be initialized to nil.
> Reference-counted types (unlike other types) cannot contain memory
> garbage. But it doesn't mean that they are always initialized empty
> when the function starts. You need to explicitly do Result := '' if
> your code reads the Result later.
 > See similar questions for Delphi:


I never dreamed that

local_var := some_function (param);

can result in the old value of local_var being used inside 
some_function. That's what's happening here. I fully expected the result 
dynamic array (embedded in a record type) to be initialized to nil, and 
have never taken care to initialize dynamic arrays and ansistrings. I'm 
astonished that this didn't bite me sooner, actually. Perhaps it is a 
culprit in occasional crashes.

I can't see any mention of this in the documentation:

> Luckily, FPC warns about it, at least in my simple test:

it also warns me in a simple test, but it doesn't give any warning in my 
not-simple program.

Is there any compiler option that I can use to force nil initialization 
of ref-counted function results?


More information about the fpc-pascal mailing list