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:
> http://stackoverflow.com/questions/5336863/what-is-the-default-value-of-result-in-delphi > http://stackoverflow.com/questions/5314918/do-i-need-to-setlength-a-dynamic-array-on-initialization/5315254#5315254

OUCH!!

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:
http://www.freepascal.org/docs-html/ref/refse47.html
http://www.freepascal.org/docs-html/ref/refse89.html
http://www.freepascal.org/docs-html/ref/refse91.html

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?


Thanks,
David



_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply via email to