[fpc-devel] Access Violation withnested DLL'scompiledbyFPC(andsome more info on bug #4538)

Marc Weustink marc.weustink at cuperus.nl
Thu Dec 15 10:49:10 CET 2005

L505 wrote:
>>In short (don't pin me on the names or on exact details in special cases):
there was a reason I said this.

>>Assume you have a ansistring and you assign something to it
>>   S := 'SomeString';
>>the compiler generates something like
>>   DecStringRef(S);
>>   S -> 'SomeString';
>>   IncStringRef(S);
>>The DecStringRef() decrements the refcount and checks if the it reaches
>>zero. Ifso, the string is freed. The referencecounter of a strings lies
> Why does it generate a DecStringRef before you assigned the string? What if it is the
> first time and you are already at 0, it can't decrement it to -1 can it?
> Does the reference count start at 0, or 1?

A string is initially initialized as nil. DecStringRef checks if it is a 
nil, if so, nothing is done.

> In this case, it decrements 1 and increments 1, so we will always end up with a very
> simple, easy, solvable problem.
> 1 - 1 = 0
> 1 - 1 = 0

Nope, since the string was nil, it isn't decremented, so you end up with 
a refcount of 1

> That seems like it isn't doing anything useful, since we are incrementing and
> decrementing by one, every time we assign a string.
> What Jonas said was that the reference count would be 1 (one) when you pass it to a
> DLL. You are saying it will be 0 (zero). 

I didn't say that. You assumed that the initial string was empty.

> We have two people saying opposite things
> here, so I'm really not sure what is going on. What would be logical to me, is if the
> reference count was 1, not zero.

It is 1.


> I guess when I finally understand this science, I will write a diagram showing how it
> works in a Pascal procedure or function with comments beside each code snippet saying
> what the reference count is.
> If there is a PDF file that explains this or some good website let me know. I hate to
> embarrass myself here (maybe other people are confused too, though).

More information about the fpc-devel mailing list