Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7039741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:50:05+00:00 2026-05-28T01:50:05+00:00

Googlemock is incorrectly reporting problem at test exit. What am I doing wrong? I

  • 0

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.

The error I get is:

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.

The code for a simple test is:

#include <string>
#include <iostream>
#include <memory>
#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_->b();
    return bar_->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<Foo> newFoo(new Foo(&barMock));

        EXPECT_CALL(barMock, b());
        EXPECT_CALL(barMock, c("hello")).WillOnce(Return(42));

        newFoo->a();
    }
    catch (std::exception& e)
    {
        std::cout << "Mock exception caught: " << e.what() << std::endl;
    }
    catch (...)
    {
    }
}

int main(int argc, char* argv[])
{
    ::testing::GTEST_FLAG(throw_on_failure) = true;
    ::testing::InitGoogleMock(&argc, &argv[0]);
    TestAFunctionInFoo();
    return 0;
}

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.

Thank you very, very much for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T01:50:06+00:00Added an answer on May 28, 2026 at 1:50 am

    It happens because you have enabled throw_on_failure flag – as it’s mentioned in Google Mock ForDummies:

    This approach has a catch: it makes Google Mock throw an exception from a mock object’s destructor sometimes.

    Since an exception is thrown from destructor, mock object is never fully deleted and framework complains about it when program ends.

    The simplest solution to this problem is to use Google Test as unit testing framework and have throw_on_failure flag disabled (default state) – GTest automatically fails test if mock expectations are not met within.

    If you cannot use Google Test because you’re using another framework (e.g. CppUnit) you can rewrite your test body this way:

    void TestAFunctionInFoo()
    {
        ::testing::GTEST_FLAG(throw_on_failure) = true;
        BarMock barMock;
    
        try
        {
            std::unique_ptr<Foo> newFoo(new Foo(&barMock));
    
            EXPECT_CALL(barMock, b());
            EXPECT_CALL(barMock, c("hello")).WillOnce(Return(42));
    
            newFoo->a();
            ::testing::Mock::VerifyAndClearExpectations(&barMock);
        }
        catch (std::exception& e)
        {
            // expectations failed - do whatever you want 
        }
    
        ::testing::GTEST_FLAG(throw_on_failure) = false;
    }
    

    VerifyAndClearExpectations(void*) checks if given mock expectations are met – if not and throw_on_failure flag is enabled it throws an exception.

    This way your mock object will be destroyed properly, and you will still have an exception if something has gone wrong.

    BTW: Avoid using catch (...) { } – it’s generally bad idea, which in most cases proves poor code quality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be a good design pattern if I require to test a C++
I'm writing a C++ class to wrap sockets (I know that there are good
I have a problem with one of my googletest unittests that uses a mock
I'm using the Boost::Test library for unit testing, and I've in general been hacking
I just bought a Galaxy Nexus, now I got a problem, my ADB doesn't
We have just started our new assignment - web-based project. Before I get directly
Using Visual Studio 2010 C++ with googlemock. I'm trying to use a mock I
I asked this question on the Google Group but I think I will get
When I define my test as follows it works. TEST(MyService, WhenCalled_DoesTheRightThingTM) { // Arrange
Can a mock class inherit from another mock class in googlemock? If yes, then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.