google / google/googletest

Mock object leaks after EXPECT_CALL and EXPECT_EXIT with -1

Open
#3,828 4 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
C++
Stars
39.6k
Forks
10.9k
Avg merge
6d 13h
Merged PRs (30d)
1

Description

**Describe the bug**

After I call `EXPECT_CALL` on a mock object and then call a function that returns -1 from `EXPECT_EXIT` I get a message about an object that should be deleted but never being:
```
somefile.cpp:198: ERROR: this mock object (used in test TestDummy.dummy_test) should be deleted but never is. Its address is @0x22c8c60.
ERROR: 1 leaked mock object found at program exit. Expectations on a mock object are verified when the object is destructed. Leaking a mock means that its expectations aren't verified, which is usually a test bug. If you really intend to leak a mock, you can suppress this error using testing::Mock::AllowLeak(mock_object), or you may use a fake or stub instead of a mock.
```

**Steps to reproduce the bug**

Following is a MRE:
```cpp
class Dummy
{
public:
virtual ~Dummy() = default;
virtual bool bar() = 0;
};

class DummyMock : public Dummy
{
public:
~DummyMock()
{
std::cout << "something" << std::endl;
}
MOCK_METHOD(bool, bar, (),(override));
};

class TestDummy : public testing::Test
{
protected:
TestDummy()
: mock(DummyMock())
{

}
DummyMock mock;
};

int foo()
{
exit(1);
}

TEST_F(TestDummy, dummy_test)
{
EXPECT_CALL(mock, bar()).Times(1).WillOnce(Return(true));
EXPECT_EXIT(foo(), testing::ExitedWithCode(1), "");
}
```

**Does the bug persist in the most recent commit?**

This is the latest release 1.11

**What operating system and version are you using?**
Centos 7:
`Linux 3.10.0-1062.12.1.rt56.1042.el7.x86_64 #1 SMP PREEMPT RT Wed Feb 5 10:31:05 CET 2020 x86_64 x86_64 x86_64 GNU/Linux`

**What compiler and version are you using?**

```
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/opt/rh/devtoolset-8/root/usr/libexec/gcc/x86_64-redhat-linux/8/lto-wrapper
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/opt/rh/devtoolset-8/root/usr --mandir=/opt/rh/devtoolset-8/root/usr/share/man --infodir=/opt/rh/devtoolset-8/root/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --with-default-libstdcxx-abi=gcc4-compatible --enable-plugin --enable-initfini-array --with-isl=/builddir/build/BUILD/gcc-8.3.1-20190311/obj-x86_64-redhat-linux/isl-install --disable-libmpx --enable-gnu-indirect-function --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux
Thread model: posix
gcc version 8.3.1 20190311 (Red Hat 8.3.1-3) (GCC)
```

**What build system are you using?**
`cmake3 version 3.17.5`

Contributor guide

Open the contributing guide

Research direction

Start by running the provided C++ MRE with EXPECT_CALL and EXPECT_EXIT, using the stated GoogleTest 1.11 environment as a reference. Trace the mock lifetime and EXPECT_EXIT behavior, then verify that the test exits as expected without reporting a leaked mock and still verifies its expectation.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.