title: When should I provide a destructor for my class?
body: <p>This seems like a rather trivial or at least common question, but I couldn't find a satisfying answer on google or on SO.</p>
  
  <p>I'm not sure when I should implement a destructor for my class.</p>
  
  <p>An obvious case is when the class wraps a connection to a file, and I want to make sure the connection is closed so I close it in the destructor.</p>
  
  <p>But I want to know in general, how can I know if I should define a destructor. What guidelines are there that I can check to see if I should have a destructor in this class?</p>
  
  <p>One such guideline I can think of, is if the class contains any member pointers. The default destructor would destory the pointers on deletion, but not the objects they're pointing at. So that should be the work of a user-defined destructor. E.g: (I'm a C++ newbie, so this code might not compile).</p>
  
  <pre><code>class MyContainer {
  public:
      MyContainer(int size) : data(new int[size]) { }
      ~MyContainer(){
          delete [] data;
      }
      // .. stuff omitted
  private:
      int* data;
  }
  </code></pre>
  
  <p>If I hadn't supplied that destructor, than destroying a <code>MyContainer</code> object would mean creating a leak, since all the data previously referenced by <code>data</code> wouldn't have been deleted.</p>
  
  <p>But I have two questions:</p>
  
  <p>1- Is this the only 'guideline'? I.e. define a destructor if the class has member pointers or if it's managing a resource? Or is there anything else?</p>
  
  <p>2- Are there cases when I should <em>not</em> <code>delete</code> member pointers? What about references?</p>