title: Googlemock incorrectly reporting error at test exit. What am I doing wrong?
body: <p>Googlemock is incorrectly reporting problem at test exit.  What am I doing wrong?  I have scoured the googlemock documentation, but there is no good complete example, and nothing describing the problem I am having.</p>
  
  <p>The error I get is:</p>
  
  <pre><code>googlemocktest.cpp(53): ERROR: this mock object should be deleted but never is.
  Its address is @0018FDC4.
  ERROR: 1 leaked mock object found at program exit.
  </code></pre>
  
  <p>The code for a simple test is:</p>
  
  <pre><code>#include &lt;string&gt;
  #include &lt;iostream&gt;
  #include &lt;memory&gt;
  #include "gmock/gmock.h"
  
  class IBar
  {
  public:
      virtual ~IBar() {}
      virtual void b() = 0;
      virtual int c(std::string) = 0;
  };
  
  class Foo
  {
  private:
      IBar *bar_;
  public:
      Foo(IBar *bar);
      int a();
  };
  
  Foo::Foo(IBar *bar)
      : bar_(bar)
  {
  }
  
  int Foo::a()
  {
  //  bar_-&gt;b();
      return bar_-&gt;c("hello");
  }
  
  class BarMock : public IBar
  {
  public:
      MOCK_METHOD0(b, void());
      MOCK_METHOD1(c, int(std::string));
  };
  
  using ::testing::Return;
  
  void TestAFunctionInFoo()
  {
      try
      {
          BarMock barMock;
          std::unique_ptr&lt;Foo&gt; newFoo(new Foo(&amp;barMock));
  
          EXPECT_CALL(barMock, b());
          EXPECT_CALL(barMock, c("hello")).WillOnce(Return(42));
  
          newFoo-&gt;a();
      }
      catch (std::exception&amp; e)
      {
          std::cout &lt;&lt; "Mock exception caught: " &lt;&lt; e.what() &lt;&lt; std::endl;
      }
      catch (...)
      {
      }
  }
  
  int main(int argc, char* argv[])
  {
      ::testing::GTEST_FLAG(throw_on_failure) = true;
      ::testing::InitGoogleMock(&amp;argc, &amp;argv[0]);
      TestAFunctionInFoo();
      return 0;
  }
  </code></pre>
  
  <p>I have verified with a debugger that IBar::~IBar() is indeed getting called.  But I still get this error message.  If I uncomment the call to IBar::b() in Foo::a(), then there is no test failure so the catch statement doesn't get called.  IBar::~IBar() is called, but there is no error message indicating a mock object isn't deleted.</p>
  
  <p>Thank you very, very much for your help!</p>