title: Deleting dynamic objects with a destructor? C++
body: <p>I have a few objects throughout the program that are facing a similar issue.  One example:</p>
  
  <p>I have an Image class:</p>
  
  <pre><code>class Image
  {
  public:
      Image();
      Image(const Image&amp; copy);
      ~Image();
  
      void loadimage(string filename);
      void saveimage(string filename);
      Image superimpose(const Image&amp; ontop, Color mask);
  
      int getwidth();
      int getheight();
      Image operator=(const Image&amp;);
  
  protected:    
      Color** pixels;
      int width;
      int height;
      ImageLoader* loader;
  };
  </code></pre>
  
  <p>It has a copy constructor:</p>
  
  <pre><code>Image::Image(const Image&amp; copy)
  {
      width = copy.width;
      height = copy.height;
      loader = copy.loader;
  
      pixels = new Color*[height];
      for(int i = 0; i &lt; height; i++)
      {
          pixels[i] = new Color[width];
      }
  
      for(int h = 0; h &lt; height; h++)
      {
          for(int w = 0; w &lt; width; w++)
          {
              pixels[h][w] = copy.pixels[h][w];
          }
      }
  }
  </code></pre>
  
  <p>Color is a struct:</p>
  
  <pre><code>struct Color
  {
      unsigned int r;
      unsigned int g;
      unsigned int b;
  };
  </code></pre>
  
  <p>My concern is that I create a dynamic two-dimensional array of Color structs, but I'm not sure when and where to delete it.  I implemented the following in my Image destructor, but I'm not 100% certain that it's doing the job and I'm not sure how to check if it does:</p>
  
  <pre><code>Image::~Image()
  {
      for(int i = 0; i &lt; height; i++)
      {
          delete[] pixels[i];
      }
  
      delete[] pixels;
      pixels = NULL;
  }
  </code></pre>
  
  <p>Am I implementing the memory deallocation correctly?</p>