title: Google mock global mock object memory leak
body: <p>I am using VS2005, and C++ for unit testing using google mock.<br>
  I had a global free function in unit testing, and I used the following code to mock free function:</p>
  
  <pre><code>NiceMock &lt;MockA&gt; mockObj;  
  
  
  
  struct IFoo {  
      virtual A* foo() = 0;  
      virtual ~IFoo() {}  
  };  
  
  struct FooMock : public IFoo {  
      FooMock() {}  
      virtual ~FooMock() {}  
      MOCK_METHOD0(foo, A*());  
  };
  
  FooMock fooMock;
  
  // foo() implementation  
  A* foo() {  
      return fooMock.foo();  
  }  
  </code></pre>
  
  <p>In the <code>SetUp()</code> function, I set Expectations on the global object like  </p>
  
  <pre><code>EXPECT_CALL(fooMock,foo())  
      .Times(1)  
      .WillOnce(Return(&amp;mockObj));  
  
  TEST(..., instA) {
  
      // ...
  }
  </code></pre>
  
  <p>and in <code>TearDown()</code>, I delete the global mock object <code>fooMock</code>  </p>
  
  <pre><code>virtual TearDown(){  
      delete &amp;fooMock;  
  }  
  </code></pre>
  
  <p>When I run the code, I get the following error  </p>
  
  <blockquote>
    <p>Error: Memory Leak in xyz.instA,</p>
  </blockquote>
  
  <p>also,<br>
  0 bytes in 0 Free Blocks.<br>
  -61 bytes in -1 Normal Blocks.<br>
  68 bytes in 7 CRT Blocks.<br>
  0 bytes in 0 Ignore Blocks.<br>
  0 bytes in 0 Client Blocks.<br>
  Largest number used: 11025 byte<br>
  Total allocations: 50602 bytes.  </p>
  
  <p>Can anyone tell me what is happening here? If I don't delete <code>fooMock</code>, I get the error "fooMock should be delete but never is", or Heap corruption detected.<br>
  From the error, I can see that somewhere my heap is being mishandled, but I cannot find the point. I have tried to debug it step by step as well.</p>
  
  <p>Some help would be really great! :)</p>