Reference counting in C++

K

Kalle Rutanen

Hello

I implemented reference counting in my program, and found out many
problems associated with it. I wonder if the following problems can be
solved automatically rather manually ?

1. In its member function, an object manages to destroy last
reference to itself and thus destructs before the end of the member
function.

In attached counted body, this can be solved manually by taking a
reference to itself in the beginning of the function. Automatically?
What about in detached counted body?

2. An object is referenced during its constructor (for example in
calling other member functions). Object is destroyed before end of the
constructor function, because there can't be any references to it yet
(assuming the reference to it was not stored).

In attached counted body, this can be solved manually by creating an
object that in its constructor adds the reference count and in its
destructor subtracts the reference count, without deleting the object if
count falls to zero. Automatically? What about in detached counted body?

3. An object with static or automatic lifetime is referenced.
4. A reference counted object is used through a pointer.

These two are something that I think that can't be solved. One must just
inform the code user to not to do this.

5. Object's pointer is assigned to more than one reference.

Clearly attached counted body solution does not have this problem, but
how to solve this with detached counted body ?

6. Program contains cyclic references.

7. When reference counting is not used, the machinery is still
present.

This is a problem with attached counted body.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top