title: C++ SmartPointers leak on self assign?
body: <p>i have small problem understanding why my smart pointer class is leaking on self assing.
  If i do something like this</p>
  
  <pre><code>SmartPtr sp1(new CSine());//CSine is a class that implements IFunction iterface
  sp1=sp1;
  </code></pre>
  
  <p>my colleagues told me that my smart pointer leaks. I added some log messages in my smart pointer to track what is going on and a test and reported this:</p>
  
  <pre><code>SmartPtr sp1(new CSine());
  -&gt;CSine constructor
  -&gt;RefCounter increment 0-&gt;1
  -&gt;RefCounter constructor
  -&gt;SmartPtr constructor
  
  sp1=sp1;
  -&gt;checks if this.RefCounter == to parameter.RefCounter, if true returns the smart pointer unmodified else modifies the object and returns it with the new values; in this case it returns true and returns the object unchanged.
  
  at the end
  -&gt;SmartPtr destructor
  -&gt;RefCounter decrement 1-&gt;0
  -&gt;RefCounter destructor
  -&gt;CSine destructor
  </code></pre>
  
  <p>i can't understand why they consider that my smart pointer leaks...any ideas?
  Thank you in advance!</p>
  
  <pre><code>class SmartPtr
  {
  private:
      RefCounter* refCnt;
      void Clear()
      {
          if(!isNull() &amp;&amp; refCnt-&gt;Decr() == 0)
              delete refCnt;
          refCnt = 0;
      };
  public:
      explicit SmartPtr();
      explicit SmartPtr(IFunction *pt):refCnt(new RefCounter(pt)){};
      SmartPtr(SmartPtr&amp; other)
      {
          refCnt = other.refCnt;
          if (!isNull())
              refCnt-&gt;Incr();
      };
      virtual ~SmartPtr(void){Clear();};
  
      SmartPtr&amp; operator=(SmartPtr&amp; other)
      {
          if(other.refCnt != refCnt)
          {
              if(!rVar.isNull())
                  other.refCnt-&gt;Incr();
              Clear();
              refCnt = other.refCnt;
          }
          return *this;
      };
  
      SmartPtr&amp; operator=(IFunction* _p)
      {
  
          if(!isNull())
          {
              Clear();
          }
          refCnt = new RefCounter(fct);
          return *this;
      };
  
      IFunction* operator-&gt;();
      const IFunction* operator-&gt;() const;
      IFunction&amp; operator*();
      const IFunction&amp; operator*() const;
      bool isNull() const { return refCnt == 0; };
  
      inline bool operator==(const int _number) const;
      inline bool operator!=(const int _number) const;
      inline bool operator==(IFunction* _other) const;
      inline bool operator!=(IFunction* _other) const;
      inline bool operator==(SmartPtr&amp; _other) const;
      inline bool operator!=(SmartPtr&amp; _other) const;
  };
  
  class RefCounter
  {
      friend class SmartPtr;
  private:
      IFunction* p;
      unsigned c;
  
      explicit RefCounter(IFunction* _p):c(0),p(_p)
      {
          if(_p != NULL)
              Incr();
          cout&lt;&lt;"RefCounter constructor."&lt;&lt;endl;
      }
      virtual ~RefCounter(void)
      { 
          cout&lt;&lt;"RefCounter destructor."&lt;&lt;endl;
          if(c == 0)
              delete p; 
      }
      unsigned  Incr()
      {
          ++c;
          cout&lt;&lt;"RefCounter increment count:"&lt;&lt;c-1&lt;&lt;" to "&lt;&lt;c&lt;&lt;endl;
          return c; 
      }
      unsigned  Decr()
      {
          if(c!=0)
          {
              --c;
              cout&lt;&lt;"RefCounter decrement count:"&lt;&lt;c+1&lt;&lt;" to "&lt;&lt;c&lt;&lt;endl;
              return c;
          }
          else
              return 0;
      }
  };
  </code></pre>