tags:

views:

151

answers:

4

Hello,

I have a class that employs a reference counting mechanism. The objects of this class are eventually destroyed by calling delete this when the reference count drops to zero. My question is: can I use local on-stack variable after delete this? Here's a more specific example:

class RefCountedClass
{
public:
    RefCountedClass(Mutex& m) :
        mutex_(m)
    {}

    .
    .
    .

private:
    Mutex& mutex_;

    void RemoveReference()
    {
        // As I understand, mutex_ will be destroyed after delete,
        // but using m is all right because it is on-stack and
        // references an external object. Am I right?
        Mutex& m = mutex_; 
        m.Acquire();

        --recount_;
        if (refcount <= 0) delete this;

        m.Release();
    }
};
+7  A: 

Yes, you may do this, as long as the member variable itself is really only a reference to an external object.

(Please forgive the previous wrong answer, I was confused about the mutex_ variable.)

Konrad Rudolph
+1 from me. I overlooked what was assigned to the local variable. I like your answer better, so I deleted mine.
John
A: 

Generally the only thing you are not allowed to call after delete this is anything that makes a reference to this. This include any non-static class member or function.

In your case, there is a good chance that RemoveReference will not work. This is becuase m points to mutex_ which no longer exists after delete this. Your best bet could well be making mutex_ static.

Edit: Although mutex_ points to an external variable which continues to exist after the class is deleted, there is no bulletproof guarantee that the compiler will not refer back to mutex_ after the class is deleted to obtain the value of the external mutex. There is a good chance that things will work as expected but I do not believe this can be guaranteed.

doron
No, `m` points not to `mutex_` but to whatever `mutex_` points to: notice that `mutex_` is a reference itself!
Konrad Rudolph
See my edit - I am assuming that a reference is treated as a pointer but being a reference, the compiler may cache the value of mutex_ and have things work as expected.
doron
If the compiler does what you speculate it might, it is broken. This is legal C++. Put another way -- how it this situation different to `int a=myIntegerMemberVariable_; delete this; doSomethingWith(a);`? Isn't it pretty clear in this case that any compiler that "optimizes" this to `doSomethingWith(this->myIntegerMemberVariable_);` is broken?
Martin B
A: 

Yes in this case. Your stack variable 'm' points to an external resource that you get in the constructor

jab
+1  A: 
**Dangerous**! This may double-delete since you test for `tmp_count <= 0`. It would be safe to test for `== 0` instead. But this example beautifully illustrates why concurrent code is so hard to get right.
Konrad Rudolph
It's dangerous if the program contains some another bug(recount_ can not be <0 otherwise); but generally I agree that == is more acceptable in this case.+1 about concurrent code. Time spend to analize and verify such a code may be 10..20..50 times greater than time spend to write it.