title: Error EXC_BAD_ACCESS in list with SmartPointer
body: <p>I'm facing this problem when I remove an element from my list.</p>
  
  <p>Here my list.h:</p>
  
  <pre><code>class AwesomeList {
  friend class Iteratore;
  private:
  class Nodo;
  
  class SmartPointer {
  public:
      Nodo* punt;
  
      SmartPointer(Nodo* n = 0): punt(n) {}
      SmartPointer(const SmartPointer&amp; ptr): punt(ptr.punt) {}
      ~SmartPointer() {
          delete punt;
      }
  
      SmartPointer&amp; operator=(const SmartPointer&amp; ptr) {
          if (this != &amp;ptr) {
              delete punt;
              punt = ptr.punt;
          }
          return *this;
      }
      bool operator==(const SmartPointer&amp; ptr) const {
          return ptr.punt == punt;
      }
      bool operator!=(const SmartPointer&amp; ptr) const {
          return ptr.punt != punt;
      }
      Nodo* operator-&gt;() const {
          return punt;
      }
      Nodo&amp; operator*() const {
          return *punt;
      }
  };
  
  class Nodo {
  public:
      T* value;
      SmartPointer next;
  
      Nodo(T* t = T(), const SmartPointer&amp; ptr = SmartPointer()): value(t), next(ptr) {}
  };
  
  SmartPointer head;
  SmartPointer tail;
  
  public:
  class Iteratore{
      friend class AwesomeList;
  private:
      AwesomeList::SmartPointer punt;
  public:
      bool operator==(const Iteratore&amp; it) const {
          return it.punt == punt;
      }
      bool operator!=(const Iteratore&amp; it) const {
          return it.punt != punt;
      }
      Iteratore&amp; operator++() {
          if(punt != 0) punt = punt-&gt;next;
          return *this;
      }
      Iteratore&amp; operator++(int) {
          if(punt != 0) punt = punt-&gt;next;
          return *this;
      }
      T* operator*() const {
          if (punt != 0) return punt-&gt;value;
      }
  };
  
  AwesomeList(const SmartPointer&amp; ptr = 0): head(ptr), tail(0) {
      if (head != 0) {
          SmartPointer p = head;
          while (p != 0)
              p = p-&gt;next;
          tail = p;
      }
  }
  AwesomeList(const AwesomeList&amp; list): head(list.head), tail(list.tail) {}
  
  AwesomeList&amp; operator=(const AwesomeList&amp; list) {
      head = list.head;
      tail = list.tail;
  }
  
  int getSize() const {
      int count = 0;
      SmartPointer p = head;
      while (p != 0) {
          p = p-&gt;next;
          count++;
      }
      return count;
  }
  bool isEmpty() const {
      return getSize() == 0;
  }
  T* at(int pos) const {
      if (pos &gt; -1 &amp;&amp; pos &lt; getSize()) {
          SmartPointer p = head;
          while (pos--) {
              p = p-&gt;next;
          }
          return p-&gt;value;
      } else return 0;
  }
  void add(const T&amp; t) {
      if (head == 0) {
          head = SmartPointer(new Nodo(&amp;(const_cast&lt;T&amp;&gt;(t))));
          tail = head;
      } else {
          tail-&gt;next = SmartPointer(new Nodo(&amp;(const_cast&lt;T&amp;&gt;(t))));
          tail = tail-&gt;next;
      }
  }
  void remove(int pos) {
      if (pos &gt; -1 &amp;&amp; pos &lt; getSize()) {
          SmartPointer newHead = head;
          SmartPointer p = newHead;
          head = 0;
          while (pos--) {
              add(*p-&gt;value);
              p = p-&gt;next;
          }
          p = p-&gt;next;
          while (p != 0) {
              add(*p-&gt;value);
              p = p-&gt;next;
          }
      }
  }
  void replace(int pos, T* t) {
      if (pos &gt; -1 &amp;&amp; pos &lt; getSize()) {
          SmartPointer p = head;
          while (pos--)
              p = p-&gt;next;
          p-&gt;value = t;
      }
  }
  void replace(int pos, const T&amp; t) {
      if (pos &gt; -1 &amp;&amp; pos &lt; getSize()) {
          SmartPointer p = head;
          while (pos--)
              p = p-&gt;next;
          T&amp; t_obj = const_cast&lt;T&amp;&gt;(t);
          p-&gt;value = &amp;t_obj;
      }
  }
  
  Iteratore begin() const {
      Iteratore it;
      it.punt = head;
      return it;
  }
  Iteratore end() const {
      Iteratore it;
      it.punt = 0;
      return it;
  }
  T* operator[](const Iteratore&amp; it) const {
      return it.punt-&gt;value;
  }
  };
  </code></pre>
  
  <p>This are the tests I made:</p>
  
  <pre><code>AwesomeList&lt;int&gt; list = AwesomeList&lt;int&gt;();
  list.add(1);
  list.add(2);
  list.add(3);
  for (int i = 0; i &lt; list.getSize(); i++)
      qDebug() &lt;&lt;*(list.at(i)) &lt;&lt;" ";
  
  list.remove(-1);
  for (int i = 0; i &lt; list.getSize(); i++)
      qDebug() &lt;&lt;*(list.at(i)) &lt;&lt;" ";
  
  list.remove(2);
  for (int i = 0; i &lt; list.getSize(); i++)
      qDebug() &lt;&lt;*(list.at(i)) &lt;&lt;" ";
  
  list.replace(0, 5);
  qDebug() &lt;&lt;"Replace in posizione 0";
  auto cit = list.begin();
  for (; cit != list.end(); cit++)
      qDebug() &lt;&lt;*(*cit) &lt;&lt;" ";
  
  qDebug() &lt;&lt;"Size";
  qDebug() &lt;&lt;list.getSize() &lt;&lt;endl;
  </code></pre>
  
  <p>This is the lines where errors appears:</p>
  
  <ul>
  <li>AwesomeList::Nodo::~Nodo() + 16 (awesomelist.h:8)</li>
  <li>AwesomeList::SmartPointer::~SmartPointer() + 42 (awesomelist.h:21)</li>
  <li>AwesomeList::SmartPointer::~SmartPointer() + 21 (awesomelist.h:22)</li>
  </ul>
  
  <p>Any help is appreciate. Thanks!</p>
  
  <p>UPDATE</p>
  
  <p>I solved my problem changing SmartPointer and Nodo classes like this:</p>
  
  <pre><code>SmartPointer(Nodo* n = 0): punt(n) {
          if (punt) punt-&gt;references++;
      }
      SmartPointer(const SmartPointer&amp; ptr): punt(ptr.punt) {
          if (punt) punt-&gt;references++;
      }
      ~SmartPointer() {
          if (punt) {
              punt-&gt;references--;
              if (punt-&gt;references == 0) delete punt;
          }
      }
  
      SmartPointer&amp; operator=(const SmartPointer&amp; ptr) {
          if (this != &amp;ptr) {
              Nodo* n = punt;
              punt = ptr.punt;
              if (punt) punt-&gt;references++;
              if (n) {
                  n-&gt;references--;
                  if (n-&gt;references == 0) delete n;
              }
          }
          return *this;
      }
      bool operator==(const SmartPointer&amp; ptr) const {
          return ptr.punt == punt;
      }
      bool operator!=(const SmartPointer&amp; ptr) const {
          return ptr.punt != punt;
      }
      Nodo* operator-&gt;() const {
          return punt;
      }
      Nodo&amp; operator*() const {
          return *punt;
      }
  };
  
  class Nodo {
  public:
      T* value;
      SmartPointer next;
      int references;
  
      Nodo(T* t = T(), const SmartPointer&amp; ptr = SmartPointer()): value(t), next(ptr), references(0) {}
  };
  </code></pre>