Memory leak when mocking static functions

483 views
Skip to first unread message

Andreas Freund

unread,
Sep 15, 2020, 2:50:08 AM9/15/20
to cpputest

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

Bas Vodde

unread,
Sep 15, 2020, 2:56:04 AM9/15/20
to cppu...@googlegroups.com
Hi,

Is it because there is a spelling mistake here: “teardwon” ?

Bas

--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cpputest/853888b9-9632-4d51-99be-079093e0f41dn%40googlegroups.com.

Andreas Freund

unread,
Sep 15, 2020, 3:17:51 AM9/15/20
to cppu...@googlegroups.com
Hi Bas,

you're right, the spelling mistake caused the memory leak. Didn't see this obvious error 😄

Thanks!

You received this message because you are subscribed to a topic in the Google Groups "cpputest" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/cpputest/ZVNrO_a0UEo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to cpputest+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cpputest/5AF242DC-C4F5-4A58-9045-C3D461F460AB%40odd-e.com.

Bas Vodde

unread,
Sep 15, 2020, 5:15:38 AM9/15/20
to cppu...@googlegroups.com

Hi,

These mistakes are often hard to notice …. you keep looking over them again and again.

Good that it solved it.

Thanks,

Bas

Steven Collins

unread,
Sep 15, 2020, 10:31:50 AM9/15/20
to cppu...@googlegroups.com
If you are using C++11 or later you can help your compiler spot this type of issue by adding the "override" keyword to your setup() and teardown() routines. If you typo the name it won't align with the method you're attempting to override and you'll get a compiler error.

TEST_GROUP(Example) {
  void setup() override {}
  void teardown() override {}
};

Reply all
Reply to author
Forward
0 new messages