title: c++ define a destructor in an iterator class?
body: <p>I've build an iterator class aiming at inspecting a binary tree. This iterator is defined bellow:</p>
  
  <pre><code>template &lt;class Key&gt; class intervalST_const_iterator
  {
  //friend class IntervalST&lt;Key&gt;;
  
  private:
      const Interval&lt;Key&gt; *interval;// equivalent to Interval&lt;Key&gt; const *interval; meaning: interval is apointer to the const object: Interval&lt;Key&gt;
                                    //meaning that the object cannot be changed via interval
                                    //interval can change here
  
  public:
      intervalST_const_iterator(const Interval&lt;Key&gt; *p): interval(p){}
      //~intervalST_const_iterator() {delete interval;}
      //the const at the end of this operator means that the operator will not change *this
      bool operator != (const intervalST_const_iterator &amp; other) const
      {
          return this-&gt;interval != other.interval;
      }
      bool operator == (const intervalST_const_iterator &amp; other) const
      {
          return this-&gt;interval == other.interval;
      }
      //the const at the beginning means that the it's not allowed to change the object
      const Interval&lt;Key&gt; operator *() const
      {
          return *(this-&gt;interval);
      }
      intervalST_const_iterator&lt;Key&gt; &amp; left()
      {
          interval = interval-&gt;left;
          return *this;
  
      }
      intervalST_const_iterator&lt;Key&gt; &amp; right()
      {
          interval = interval-&gt;right;
          return *this;
      }
  };
  </code></pre>
  
  <p>In this class I've also defined a <strong>destructor</strong> <code>~intervalST_const_iterator() {delete interval;}</code> I've put it in comment.</p>
  
  <p>I'm using this iterator in order to iterate on a tree class that looks like that:</p>
  
  <pre><code>template &lt;class Key&gt; class intervalST_const_iterator;
  
  template &lt;class Key&gt; class IntervalST
  {
  private:
      Interval&lt;Key&gt; *root;
  
      //friend class intervalST_const_iterator&lt;Key&gt;;//allow the iterator class to access the private section of intervalST
  
      bool isRed(Interval&lt;Key&gt; *interval);
      Interval&lt;Key&gt; *rotateLeft(Interval&lt;Key&gt; *h);
      Interval&lt;Key&gt; *rotateRight(Interval&lt;Key&gt; *h);
      Interval&lt;Key&gt; *put(Interval&lt;Key&gt; *h,Key lo, Key hi, Key val);
      Interval&lt;Key&gt; *moveRedLeft(Interval&lt;Key&gt; *h);
      Interval&lt;Key&gt; *moveRedRight(Interval&lt;Key&gt; *h);
      Interval&lt;Key&gt; *deleteMin(Interval&lt;Key&gt; *h, Key hi);
      Interval&lt;Key&gt; *balance(Interval&lt;Key&gt; *h);
      Interval&lt;Key&gt; *remove(Interval&lt;Key&gt; *h, Key lo, Key hi);
      Interval&lt;Key&gt; *min(Interval&lt;Key&gt; *h);
      Interval&lt;Key&gt; *addDuplicate(Interval&lt;Key&gt; *h, Key hi);
      Interval&lt;Key&gt; *removeDuplicate(Interval&lt;Key&gt; *h, Key low, Key hi);
      Interval&lt;Key&gt; *getPointerToKey(Key low);
  
      void flipColors(Interval&lt;Key&gt; *h);
      void destroy(Interval&lt;Key&gt; *h);
      void printTree(Interval&lt;Key&gt; *h, int indent);
      Key maxVal(Interval&lt;Key&gt; *h);
      int size(Interval&lt;Key&gt; *h);
      bool isBST(Interval&lt;Key&gt; *x, Key min, Key max);
      inline bool isBST(){return isBST(root,0,0);}
      bool isSizeConsistent(Interval&lt;Key&gt; *x);
      inline bool isSizeConsistent(){return isSizeConsistent(root);}
      bool is23(Interval&lt;Key&gt; *x);
      inline bool is23(){return is23(root);}
      bool isBalanced();
      bool isBalanced(Interval&lt;Key&gt; *x,int black);
      int getKeySize(Key low);
      int compare(Key a, Key b);
  
  public:
  
      //don't forget to build the constructor
      //and overload the =equal operator
      typedef intervalST_const_iterator&lt;Key&gt; const_iterator;//const iterator on a tree
  
  
      const_iterator begin() const;
      const_iterator end() const;
  
      IntervalST():root(NULL){};
      ~IntervalST();
      void remove(Key lo, Key hi);
      void put(Key lo, Key hi);
      inline int size(){return size(root);}
      inline bool isEmpty(){return root == NULL;}
      void print(int indent = 0);
      void check();
  
  
  };
  </code></pre>
  
  <p>with corresponding <code>begin()</code> and <code>end()</code> method for the iterator</p>
  
  <pre><code>template &lt;typename Key&gt; typename IntervalST&lt;Key&gt;::const_iterator IntervalST&lt;Key&gt;::begin() const
  {
  return const_iterator(root);
  }
  
  template &lt;typename Key&gt; typename IntervalST&lt;Key&gt;::const_iterator IntervalST&lt;Key&gt;::end() const
  {
  //return const_iterator(NULL,this);
  return const_iterator(NULL);
  }
  </code></pre>
  
  <p>Then I'm using a function - see bellow - which works well when the destructor of the iterator class is not defined and <strong>gives strange results when the destructor is defined</strong>.</p>
  
  <p><strong>Why</strong> is this happening? <strong>Do I need to define a destructor</strong> for the iterator class in order to prevent leaks?
  <strong>How</strong> to handle this problem? 
  thanks for your help.</p>
  
  <pre><code>template &lt;typename Key&gt; std::vector&lt;Segment&lt;Key&gt; &gt; findIntersections(const IntervalST&lt;Key&gt; &amp;tree ,Segment&lt;Key&gt; segment)
  {
  //initialize a const iterator on the tree
  intervalST_const_iterator&lt;Key&gt; iterator = tree.begin();
  vector&lt;Segment&lt;Key&gt; &gt; intersections;
  
  Key k = (*iterator).max;
  //walk the tree
  while(iterator != tree.end())
  {
      //if(*(iterator).intersects(segment.gety1(),segment.gety2())) intersections.push_back(segment);
      if(intersects((*iterator).low,(*iterator).back,segment.gety1(),segment.gety2()))
      {
          intersections.push_back(segment);
          return intersections;
      }
      else if (iterator.left() == tree.end())                                          iterator = iterator.right();
      else if (segment.gety1() &gt; (*iterator.left()).max)                               iterator = iterator.right();
      else                                                                             iterator = iterator.left();
  }
  return intersections;
  }
  </code></pre>