[fpc-devel] ref count issue with out param

Michael Van Canneyt michael at freepascal.org
Sat Jun 13 16:24:26 CEST 2015



On Sat, 13 Jun 2015, Martin Frb wrote:

> On 13/06/2015 15:12, Michael Van Canneyt wrote:
>> 
>> 
>> Note the 0 !
>> 
>> Secondly, the section about 'out' parameters is very old; there were not 
>> nearly so much managed types at the time.
>> I will update it, I was just documenting the new string types anyway. The 
>> above will nicely illustrate the point.
>
> What about the (unverified) remark on the forum that delphi does it different 
> (apparently passing nil in that case)?
>
> "const" is an exception. The purpose of const is to skip the ref counting.
>
> "Out" has a different purpose. Should it really create a dangling pointer in 
> a ref counted variable?
>
> In your example you are lucky you got a refcount of 0. The memory is 
> after-all freed. you called the StringRefCount on a dangling pointer. 
> Anything could have happen.

No, the compiler *must* initialize it. 
Without that guarantee, ansistring out parameters could never work.

If the out got a dangling pointer as input, it would never be able to decrease the ref. count when assigning something to it.
Ergo, the compiler must initialize out params.

Michael.



More information about the fpc-devel mailing list