title: Destructor seems to be called 'early'
body: <p>Turned out it was a simple constructor miss-use problem.
  <strong>Please see the "edit" section for the updated information.</strong></p>
  
  <p>Sorry for yet another C++ dtor question...
  However I can't seem to find one exactly like mine as all the others are assigning to STL containers (that will delete objects itself) whereas mine is to an array of pointers.</p>
  
  <p>So I have the following code fragment</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  class Block{
  public:
      int x, y, z;
      int type;
      Block(){
          x=1;
          y=2;
          z=3;
          type=-1;
      }
  };
  
  template &lt;class T&gt; class Octree{
      T* children[8];
  public:
      ~Octree(){
          for( int i=0; i&lt;8; i++){
              std::cout &lt;&lt; "del:" &lt;&lt; i &lt;&lt; std::endl;
              delete children[i];
          }
      }    
      Octree(){
          for( int i=0; i&lt;8; i++ )
              children[i] = new T;
      }
      // place newchild in array at [i]
      void set_child(int i, T* newchild){
          children[i] = newchild;
      }
      // return child at [i]
      T* get_child(int i){
          return children[i];
      }
      // place newchild at [i] and return the old [i]
      T* swap_child(int i, T* newchild){
          T* p = children[i];
          children[i] = newchild;
          return p;
      }
  };
  
  int main(){
      Octree&lt; Octree&lt;Block&gt; &gt; here;
      std::cout &lt;&lt; "nothing seems to have broken" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Looking through the output I notice that the destructor is being called many times before I think it should (as Octree is still in scope), the end of the output also shows:</p>
  
  <pre><code>del:0
  del:0
  del:1
  del:2
  del:3
  
  Process returned -1073741819 (0xC0000005)   execution time : 1.685 s
  Press any key to continue.
  </code></pre>
  
  <p>For some reason the destructor is going through the same point in the loop twice (0) and then dying.</p>
  
  <p>All of this occures before the "nothing seems to have gone wrong" line which I expected before any dtor was called.</p>
  
  <p>Thanks in advance :)</p>
  
  <p><strong>EDIT</strong>
  The code I posted has some things removed that I thought were unnecessary but after copying and compiling the code I pasted I no longer get the error.
  What I removed was other integer attributes of the code.
  Here is the origional:</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  class Block{
  public:
      int x, y, z;
      int type;
      Block(){
          x=1;
          y=2;
          z=3;
          type=-1;
      }
      Block(int xx, int yy, int zz, int ty){
          x=xx;
          y=yy;
          z=zz;
          type=ty;
      }
      Block(int xx, int yy, int zz){
          x=xx;
          y=yy;
          z=zz;
          type=0;
      }
  };
  
  template &lt;class T&gt; class Octree{
      int x, y, z;
      int size;
      T* children[8];
  public:
      ~Octree(){
          for( int i=0; i&lt;8; i++){
              std::cout &lt;&lt; "del:" &lt;&lt; i &lt;&lt; std::endl;
              delete children[i];
          }
      }
  
      Octree(int xx, int yy, int zz, int size){
          x=xx;
          y=yy;
          z=zz;
          size=size;
          for( int i=0; i&lt;8; i++ )
              children[i] = new T;
      }
      Octree(){
          Octree(0, 0, 0, 10);
      }
      // place newchild in array at [i]
      void set_child(int i, T* newchild){
          children[i] = newchild;
      }
      // return child at [i]
      T* get_child(int i){
          return children[i];
      }
      // place newchild at [i] and return the old [i]
      T* swap_child(int i, T* newchild){
          T* p = children[i];
          children[i] = newchild;
          return p;
      }
  };
  
  int main(){
      Octree&lt; Octree&lt;Block&gt; &gt; here;
      std::cout &lt;&lt; "nothing seems to have broken" &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>Also, as for the problems with set_child, get_child and swap_child leading to possible memory leaks this will be solved as a wrapper class will either use get before set or use swap to get the old child and write this out to disk before freeing the memory itself.</p>
  
  <p>I am glad that it is not my memory management failing but rather another error.
  I have not made a copy and/or assignment operator yet as I was just testing the block tree out, I will almost certainly make them all private very soon.</p>
  
  <p>This version spits out -1073741819.</p>
  
  <p>Thank you all for your suggestions and I apologise for highjacking my own thread :$</p>
  
  <p><strong>SOLVED</strong>
  Issue with one constructor calling another.</p>
  
  <p>Thanks all for your help and apologies for any time wasted :)</p>