C++ idiom to avoid memory leaks?
<p>In the following code, there is a memory leak if <code>Info::addPart1()</code> is called multiple times by accident:</p>

<pre><code>typedef struct
{
}part1;

typedef struct
{
}part2;

class Info
{
    private:
    part1* _ptr1;
    part2* _ptr2;    

    public:
    Info()
    {
      _ptr1 = _ptr2 = NULL;
    }

    ~Info()
    {
      delete _ptr1; 
      delete _ptr2;
    }

    addPart1()
    {
       _ptr1 = new part1;         
    }

    addPart2()
    {
      _ptr2 = new part2;         
    }   
};


Info _wrapper;
_wrapper.addPart1();
_wrapper.addPart2();
</code></pre>

<p>Is there a C++ idiom to handle this problem ? </p>

<p>I could rewrite <code>addPart1</code> and <code>addPart2</code> like this to defend the MLK </p>

<pre><code>addPart1()
{
  if(_ptr1 != NULL) delete _ptr1;
  _ptr1 = new part1;         
}
</code></pre>

<p>Is that a good solution?</p>
