Re: Lock-free reference counting



Mark Wooding wrote:
Juha Nieminen <nospam@xxxxxxxxxxxxxx> wrote:

The only way 'a' would not have a pointer to the allocated object is
if f() nulled (or whatever) that pointer.

Huh? We're discussing code of the form

ptr<foo> a = ...;
f(a);
g();
[snip]

So you are basically saying that in this hypothetical non-C++ language
what happens there is basically what in C++ would happen if you wrote:

f(ptr<foo>(new Object));
g();

(In other words, the object is created for calling the f() function
only, and destroyed immediately afterwards.)

Or, if we put that in another way:

ptr<foo> a = ...;
f(a);
// At this point the compiler sees that 'a' is not used anymore, so
// it drops it immediately after f() returns;
g();

As someone already put it, this requires compiler support to work. A
GC engine alone cannot know this. Alternatively, it needs to be a
specified language feature:

ptr<foo> a = ...;
f(a);
// 'a' has been "used up" at this point, so the following:
g(a);
// causes an error because 'a' doesn't exist anymore.

In either case I really don't see why reference counting would act any
differently (assuming that the "ptr<>" type was internally reference
counted rather than garbage-collected). Whatever the means to
drop/destroy/forget/nullify/whatever that 'a', a reference-counting
scheme would delete the object when it happens.
.