Quote Originally Posted by gioni_go View Post
Is this illegal and if yes what are the dangers. Thx in advance.
There's nothing wrong, technically, to have m_refCount(0) point to the count variable declared on the stack. But the problem with this approach is about ten-fold.
The "danger" lies in the fact that having m_refCount on the stack instead on the heap,
defeats the very purpose of reference counting of temp variable in your SmartPtr class.
Copying m_refCount will still point to the temp, but the copying of temp will not result in correct count.
In simple words,
your SmartPtr class itself needs another smart pointer semantic inside which you did not implement.
The difficulty of implementing such semantic is the complexity noted by Lindley.
(I personally have not tried it, but you can make an educated guess how hard it would be)
Code:
T* release() const {
  T* ptr = m_ptr;
  if (m_refCount) {
   (*m_refCount)++;
  }
  DEBUG("Released pointer ownership.");

  return ptr;
 }
Why are you thinking of releasing the ownership to who knows where?
That alone doesn't make sense at all.
Assuming release() will be used by SmartPtr only is a big mistake

there are other problems.
illogical order of the all-in-one call chain to ctor, reset, destroy.
incomplete implementation of copy ctor, operator=()
(all these methods themselves have problems of their own).
bottom line is that your SmartPtr class needs a complete make over as a whole.

If your c++ is little rusty,
I'd suggest that you try much simpler smart pointer class
and plainly have reference count variable on the heap.