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

Florian Klaempfl florian at freepascal.org
Thu Dec 15 09:29:04 CET 2005

L505 wrote:
>>In short (don't pin me on the names or on exact details in special cases):
>>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?

It is first incremented and then decremented. Reason: multi threading safeness.

> Does the reference count start at 0, or 1?


> 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
> That seems like it isn't doing anything useful, since we are incrementing and
> decrementing by one, every time we assign a string.

One string is incremented, the other decremented. Your conclusion applies only to

> 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). 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.
> 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).

Read the sources :)

More information about the fpc-devel mailing list