Global mock object glibc double free error

1,271 views
Skip to first unread message

hoj

unread,
Sep 2, 2011, 6:41:32 PM9/2/11
to Google C++ Mocking Framework
Hi,
I have some legacy C code I am trying to wrap with mocks which I've
structured like the example code below. The global object I use
results in a warning if I leave out Mock::AllowLeak(), but if I do add
the AllowLeak, I get:
*** glibc detected *** ./gmtest: double free or corruption (!prev)
and a backtrace and memory map. In both cases, the test itself shows
PASSED, but the return value is non-zero.

Is there a way to get this to pass cleanly?

I am using gmock 1.6.0 with g++ 4.5.2.

Thanks in advance!,
hoj

//====================================
#include "gtest/gtest.h"
#include "gmock/gmock.h"

using testing::Mock;

class mock1
{
public:
mock1() {};
MOCK_METHOD0( mock1_Init, void (void));
MOCK_METHOD0( mock1_DoStuff, void (void));
};
mock1 g_mock1;

class TestClass : public ::testing::Test
{
public:
virtual void SetUp()
{
EXPECT_CALL(g_mock1, mock1_Init()).Times(1);
g_mock1.mock1_Init();
};
virtual void TearDown() {};
};

TEST_F(TestClass, a_test)
{
EXPECT_CALL(g_mock1, mock1_DoStuff()).Times(1);
g_mock1.mock1_DoStuff();
Mock::VerifyAndClearExpectations(&g_mock1);
//Mock::AllowLeak(&g_mock1);
}

Vlad Losev

unread,
Sep 5, 2011, 2:31:46 PM9/5/11
to Google C++ Mocking Framework
Check out http://code.google.com/p/googlemock/wiki/ForDummies:

"Important note: Google Mock requires expectations to be set before the mock functions are called, otherwise the behavior is undefined. In particular, you mustn't interleave EXPECT_CALL()s and calls to the mock functions."

HTH,
Vlad

John Faith

unread,
Sep 6, 2011, 11:56:54 AM9/6/11
to Google C++ Mocking Framework
(Oops. CC'ing list)

Hi Vlad,
Is it less undefined if all expectations are matched with calls to
VerifyAndClearExpectations()? So: expectation is set, a call is made,
then VerifyAndClearExpectations, then a second expectation, a call,
then another VerifyAndClearExpectations I tried it for the example
below and it failed, so I assume not.

Another approach I'm taking is to replace all global objects with
pointers and functions to allocate the objects in SetUp. This seems
to be working so far.

Thanks,
hoj

Vlad Losev

unread,
Sep 6, 2011, 3:38:47 PM9/6/11
to Google C++ Mocking Framework
On Tue, Sep 6, 2011 at 8:56 AM, John Faith <jfa...@gmail.com> wrote:
(Oops. CC'ing list)

Hi Vlad,
Is it less undefined if all expectations are matched with calls to
VerifyAndClearExpectations()?  So: expectation is set, a call is made,
then VerifyAndClearExpectations,  then a second expectation, a call,
then another VerifyAndClearExpectations  I tried it for the example
below and it failed, so I assume not.

You can try Mock::VerifyAndClear(), which also clears out the default actions.  

 
Another approach I'm taking is to replace all global objects with
pointers and functions to allocate the objects in SetUp.  This seems
to be working so far.

I advise you should avoid using global mock objects. If they are destroyed after Google Mock's mock object registry, you may see crashes just like that one. It's better to put mocks into your test fixture classes instead. 
Reply all
Reply to author
Forward
0 new messages