[fpc-devel] ref count issue with out param

Michael Van Canneyt michael at freepascal.org
Sat Jun 13 16:42:33 CEST 2015



On Sat, 13 Jun 2015, Martin Frb wrote:

> On 13/06/2015 15:24, Michael Van Canneyt wrote:
>> 
>> 
>> 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.
>
> Yes you are right. But that makes you example different. the value of the 
> "out" param passed in is nil (because of the initialization). So the refcount 
> of 0 is no surprise at all.

The example was just to illustrate what happens in general.

>
> In my example the out param is fine too, but the other param (that gets 
> passed the same param on the callee side) is dangling.

Normally only if the initial reference count was 1.

And now you know why reference counts can be dangerous and bite you in the lef:-)

But I will see if I can document the case.

Michael.



More information about the fpc-devel mailing list