title: Why will std::function call destructor when an object was bound to a member function?
body: <pre><code>#include &lt;functional&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  class test {
  public:
    test(){ p = new int[10];}
   void print_info(double a)
    {
      cerr &lt;&lt; a &lt;&lt; endl;
    }
    ~test(){
      cerr &lt;&lt; __LINE__ &lt;&lt; endl;
      delete []p;
    }
  private:
    int *p;
  };
  
  int main()
  {
      test t;
      std::function&lt;void(void)&gt; f = std::bind(&amp;test::print_info, t, 2.0);
      //std::function&lt;void(void)&gt; f = std::bind(&amp;test::print_info, std::cref(t), 2.0);
      return 0;
  }
  </code></pre>
  
  <p>It will crash, since <code>test::~test()</code> is called twice. But, if I replace <code>t</code> with <code>std::cref(t)</code> (or <code>std::ref(t), &amp;t</code>),  <code>~test()</code> will be called only once when exiting main().</p>
  
  <p>I didn't figure out the reason. I'm on Ubuntu 12.04 64bit, using gcc 4.6.3.</p>