title: std::pair and class destructors
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three">What is The Rule of Three?</a>  </p>
  </blockquote>
  
  
  
  <p>How exactly does <code>std::pair</code> call destructors for its components?  I am trying to add instances of a class to an <code>std::map</code>, but I am getting errors regarding the destructor of my class.</p>
  
  <p>I have narrowed down my question/problem to the following extremely simple example.</p>
  
  <p>Below, <code>my_class</code> merely creates an <code>int</code> array at construction, and deletes it at destruction.  Somehow I am getting a "double delete" error:</p>
  
  <pre><code>//my_class.h
  class my_class {
    public:
      int an_int;
      int *array;
  
      //constructors:
      my_class()
      {
        array = new int[2];
      }
      my_class(int new_int) : an_int(new_int)
      {
        array = new int[2];
      }
  
      //destructor:
      ~my_class()
      {
        delete[] array;
      }
  };  //end of my_class
  </code></pre>
  
  <p>Meanwhile, over in main.cpp...</p>
  
  <pre><code>//main.cpp
  int main(int argc, char* argv[])
  {
    std::map&lt;int, my_class&gt;   my_map;
  
    my_map.insert( std::make_pair&lt;int, my_class&gt; (1, my_class(71) ) );
  
    return 0;
  } // end main
  </code></pre>
  
  <p>Compilation goes fine, but this generates the following runtime error:</p>
  
  <pre><code>*** glibc detected *** ./experimental_code: double free or corruption (fasttop):
  </code></pre>
  
  <p>Or, with valgrind:</p>
  
  <pre><code>==15258== Invalid free() / delete / delete[] / realloc()
  ==15258==    at 0x40249D7: operator delete[](void*) (vg_replace_malloc.c:490)
  ==15258==    by 0x8048B99: main (my_class.h:38)
  ==15258==  Address 0x42d6028 is 0 bytes inside a block of size 8 free'd
  ==15258==    at 0x40249D7: operator delete[](void*) (vg_replace_malloc.c:490)
  ==15258==    by 0x8048B91: main (my_class.h:38)
  </code></pre>
  
  <p>(line numbers are off because I cut out comments and stuff)</p>
  
  <p>I must be missing something about <code>std::pair</code>...? </p>
  
  <p>Thanks to all in advance!</p>