title: GMock leaking memory
body: <p><strong>Code</strong></p>
  
  <pre><code>class A {
   public:
    void doit() {...}
  }
  
  class B {
   public:
    explicit B(unique_ptr&lt;A&gt; a): a_(move(a)) {}
  
    void run() {
      a_-&gt;doit();
    }
  
   private:
    unique_ptr&lt;A&gt; a_;
  }
  </code></pre>
  
  <p><strong>Test Code</strong></p>
  
  <pre><code>class MockA : public A {
   public:
  
    MOCK_METHOD0(doit, void(void));
  }
  
  TEST(BTest, Test) {
    auto mockA = std::make_unique&lt;A&gt;();
    EXPECT_CALL(*mockA, doit(_)).Times(1);
  
    B b(std::move(mockA));
    b.run();
  }
  </code></pre>
  
  <p>When running this code it leaks </p>
  
  <p>ERROR: this mock object (used in test BTest.Test) should be deleted but never is. Its address is @0x1234.</p>
  
  <p>Since expectations are supposed to run during destruction I am not sure why its causing issues.</p>
  
  <p>I tried shared_ptr alias method as described here - <a href="https://stackoverflow.com/questions/40508033/dependency-injection-with-unique-ptr-to-mock">Dependency injection with unique_ptr to mock</a> but even still I get that exception.</p>