Memory leak in Mock Support?

734 views
Skip to first unread message

Bryce Schober

unread,
Mar 30, 2012, 6:31:46 PM3/30/12
to cppu...@googlegroups.com
Is anyone else having memory errors using the mocking? I re-constructed a brain-dead test project using the mocking support and get memory leak errors:

<Makefile>
#Comment this out for noisy make output
SILENCE = @

#--- Configuration ----#
COMPONENT_NAME = Foo
CPPUTEST_HOME = ../../../../tools/cpputest
CPP_PLATFORM = Gcc
CPPUTEST_USE_EXTENSIONS = Y
CPPUTEST_USE_GCOV = Y

#--- Inputs for build ----#
TEST_SRC_DIRS = .
INCLUDE_DIRS = $(CPPUTEST_HOME)/include

include $(CPPUTEST_HOME)/build/MakefileWorker.mk
</Makefile>

<foo_tests.cpp>
//CppUTest includes should be after your and system includes
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"

TEST_GROUP( Foo )
{
};

void foo( int bar, int *baz ) 
{
    mock().actualCall("foo");
}

TEST( Foo, NotMuch )
{
    mock().expectOneCall("foo");
    foo( 0, NULL );
    mock().checkExpectations();
}
</foo_tests.cpp>

<AllTests.cpp>
#include "CppUTest/CommandLineTestRunner.h"
#include "CppUTest/TestPlugin.h"
#include "CppUTest/TestRegistry.h"

int main(int ac, char** av)
{
return CommandLineTestRunner::RunAllTests(ac, av);
}

IMPORT_TEST_GROUP( Foo );
</AllTests.cpp>

<output of "make all">
Making clean
compiling AllTests.cpp
compiling foo_tests.cpp
Building archive lib/libFoo.a
ar: creating lib/libFoo.a
Linking Foo_tests
Running Foo_tests

foo_tests.cpp:17: error: Failure in TEST(Foo, NotMuch)
Memory leak(s) found.
Alloc num (8) Leak size: 40 Allocated at: src/CppUTestExt/MockSupport.cpp and line: 151. Type: "new"
Memory: <0x9a3d448> Content: "hv�ӣ � "
Alloc num (7) Leak size: 8 Allocated at: src/CppUTestExt/MockExpectedFunctionsList.cpp and line: 104. Type: "new"
Memory: <0x9a3d418> Content: "@ӣ "
Alloc num (5) Leak size: 64 Allocated at: src/CppUTestExt/MockSupport.cpp and line: 130. Type: "new"
Memory: <0x9a3d340> Content: "�z�ӣ �"
Alloc num (6) Leak size: 4 Allocated at: src/CppUTestExt/MockExpectedFunctionCall.cpp and line: 39. Type: "new"
Memory: <0x9a3d3e8> Content: ""
Total number of leaks:  4
.
Errors (1 failures, 1 tests, 1 ran, 0 checks, 0 ignored, 0 filtered out, 0 ms)
</output of "make all">

My gcc version is 4.3.4, for what it's worth.

<><  <><  <><
Bryce Schober

Ja...@grenning.net

unread,
Mar 30, 2012, 8:13:57 PM3/30/12
to cppu...@googlegroups.com
You need to clear() the mock

From my HTC Sensation 4G on T-Mobile. The first nationwide 4G network

Bryce Schober

unread,
Mar 30, 2012, 8:37:18 PM3/30/12
to cppu...@googlegroups.com
I will claim ignorance and a lack of such detail in the online docs: http://www.cpputest.org/node/31.

Now it works like a charm. Seems like that example's TEST_GROUP should be updated with a:
void teardown() { mock().clear(); }

<><  <><  <><
Bryce Schober

Bas Vodde

unread,
Mar 31, 2012, 12:58:17 AM3/31/12
to cppu...@googlegroups.com

Hi Bryce,

I updated the documentation on the link you send me.

The reason why we keep forgetting it is that most people use the MockSupportPlugin when starting to use the mocking framework.
That will automatically do the checking and the clearing, so it ain't needed.

You can see how to turn this on at:
http://cpputest.org/node/39

Hope this helps!

Bas

Reply all
Reply to author
Forward
0 new messages