title: Reference counting - internal references problem
body: <p>I have implemented my own smart pointers, and everything worked fine, untill I realized a fatal flaw with my implementation. The problem was the fact that an object can have a smart pointer that potentially holds a reference to itself. the problem would be easy to avoid if this was a one layer problem - what could easly happen is a ref counted class would indirectly (through one of its members) holds a referenece to itself. this would mean that a object would never be removed deleted. Is there any way/method I could solve this?</p>
  <p>simplest example:</p>
  <pre><code>class Derived : public Object {
  public:
      static ref&lt;Object&gt; Create() { return ref&lt;Object&gt;(new Derived()); }
  
  private:
      Derived() : m_ref(this) // m_ref now holds a reference to Derived instance
      {
          // SOME CODE HERE
      }
      ref&lt;Object&gt; m_ref;
  };
  </code></pre>
  <p>Object is base class contains reference counter, ref is smart pointer that holds a reference to its assigned object</p>