title: Crash When Deleting Pointer in Destructor
body: <p>I've bumped up against my lack of deep understanding of pointers in C++. I've written a class called Skymap, which has the following definition:</p>
  
  <pre><code>class Skymap {
   public:
    Skymap();
    ~Skymap();
    void DrawAitoffSkymap();
   private:
    TCanvas mCanvas;
  
    TBox* mSkymapBorderBox;
  };
  </code></pre>
  
  <p>with the functions defined as:</p>
  
  <pre><code>#include "Skymap.h"
  
  Skymap::Skymap()
  {
    mCanvas.SetCanvasSize(1200,800);
    mMarkerType=1;
  }
  
  Skymap::~Skymap()
  {
    delete mSkymapBorderBox;
  }
  
  void Skymap::DrawAitoffSkymap()
  {
    TBox* mSkymapBorderBox=new TBox(-200,-100,200,100);
    //Use the mSkymapBorderBox pointer for a while
  }
  </code></pre>
  
  <p>(I am using the ROOT plotting package, but I think this is just a general C++ question).</p>
  
  <p>Now, the following program will crash upon reaching the destructor of skymap2:</p>
  
  <pre><code>int main(){
    Skymap skymap1;
    Skymap skymap2;
    skymap1.DrawAitoffSkymap();
    skymap2.DrawAitoffSkymap();
    return(0);
  }
  </code></pre>
  
  <p>However, the following will not crash:</p>
  
  <pre><code>int main(){
    Skymap skymap1;
    skymap1.DrawAitoffSkymap();
    return(0);
  }
  </code></pre>
  
  <p>Also, if I initialise the pointer mSkymapBorderBox to NULL in the constructor, I no longer experience a crash following the execution of the first program (with 2 Skymap objects).</p>
  
  <p>Can anyone please explain what the underlying cause of this is? It appears to be a problem with the pointer in the second Skymap object, but I do not see what it is.</p>