Hi all,
I'm fairly new to CppUTest/CppUMock and Unit Testing in general. I lastly tried to mock a static function which is called via a function pointer, but this approach resulted in a memory leak.
Here's a short example:
#include "fp_test.h"
#include "CppUTestExt/MockSupport_c.h"
static void foo(void);
void (*get_fp(void))(void)
{
return &foo;
}
static void foo(void)
{
mock_c()->actualCall("foo");
}
And the Unit-Test:
#include "CppUTest/TestHarness.h"
#include "CppUTestExt/MockSupport.h"
extern "C"
{
#include "fp_test.h"
}
TEST_GROUP(MockingTests)
{
void setup()
{
}
void teardwon()
{
mock().clear();
}
};
TEST(
MockingTests
, Init)
{
mock().expectOneCall("foo");
void (*fp)(void) = get_fp();
CHECK(fp != NULL);
(fp)();
mock().checkExpectations();
}
And a part of the resulting output:
Failure in TEST(
MockingTests
, Init)
Memory leak(s) found.
Alloc num (6) Leak size: 8 Allocated at: ../src/CppUTestExt/MockExpectedCall.cpp and line: 71. Type: "new"
Memory: <0x55e63c1b85f0> Content:
0000: 00 00 00 00 00 00 00 00 |........|
Alloc num (7) Leak size: 16 Allocated at: ../src/CppUTestExt/MockExpectedCallsList.cpp and line: 115. Type: "new"
Memory: <0x55e63c1b8650> Content:
0000: 20 84 1b 3c e6 55 00 00 00 00 00 00 00 00 00 00 | ..<.U..........|
Alloc num (8) Leak size: 88 Allocated at: ../src/CppUTestExt/MockSupport.cpp and line: 177. Type: "new"
Memory: <0x55e63c1b86b0> Content:
[...]
I don't really understand why there's a memory leak since there are no heap allocations in the mentioned code. Can anyone explain this behavior and is there a way to still mock the static functions?
Greetings
Andi