11

I'm surprised that Google C++ Testing Framework does not explicitly support checking for memory leaks. There is, however, a workaround for Microsoft Visual C++, but what about Linux?

If memory management is crucial for me, is it better to use another C++ unit-testing framework?

Andrew
  • 197
  • 2
  • 3
  • 16

5 Answers5

7

If memory management is crucial for me, is it better to use another C++ unit-testing framework?

i don't know about c++ unit-testing, but i used Dr. memory, it works on linux windows and mac if you have the symbols it even tells you in what line the memory leak happened! really usefull :D
more info http://drmemory.org/

Jotac0
  • 126
  • 4
6

Even if this thread is very old. I was searching for this lately.

I now came up with a simple solution (inspired by https://stackoverflow.com/a/19315100/8633816)

Just write the following header:

#include "gtest/gtest.h"
#include <crtdbg.h>

class MemoryLeakDetector {
public:
    MemoryLeakDetector() {
        _CrtMemCheckpoint(&memState_);
    }

    ~MemoryLeakDetector() {
        _CrtMemState stateNow, stateDiff;
        _CrtMemCheckpoint(&stateNow);
        int diffResult = _CrtMemDifference(&stateDiff, &memState_, &stateNow);
        if (diffResult)
            reportFailure(stateDiff.lSizes[1]);
    }
private:
    void reportFailure(unsigned int unfreedBytes) {
        FAIL() << "Memory leak of " << unfreedBytes << " byte(s) detected.";
    }
    _CrtMemState memState_;
};

Then just add a local MemoryLeakDetector to your Test:

TEST(TestCase, Test) {
    // Do memory leak detection for this test
    MemoryLeakDetector leakDetector;

    //Your test code
}

Example:

A test like:

TEST(MEMORY, FORCE_LEAK) {
    MemoryLeakDetector leakDetector;

    int* dummy = new int;
}

Produces the output:

enter image description here

I am sure there are better tools out there, but this is a very easy and simple solution.

Muperman
  • 344
  • 2
  • 14
  • 2
    I would love for your solution to work for Linux, but it only works for Windows because _CrtMem* are Windows native APIs – Eugenio Miró Sep 16 '22 at 13:05
4

"I'm surprised that Google C++ Testing Framework does not explicitly support checking for memory leaks."

It's not (and never was) purposed to do so. You can actually do some certifying, e.g. using google mock and setting up expected calls (for e.g. destructors). But using a tool specialized upon this aspect, will certainly do better, than everything you're able to write yourself.

"is it better to use another C++ unit-testing framework?"

So why bothering looking for different unit testing frameworks (that won't support such feature either, at least there's none I know of).

There are tools like valgrind you can use, and run your UnitTester executable under their control to detect memory leaks.

Note:
The above advice to do this with the UnitTester executable, won't be able to catch all of the possible memory leaks from the final executable produced with your code, but just help to find bugs/flaws with the actually tested code.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • Yeah, but this is about running my tests under another testing framework, which is probably too complicated. – Andrew Mar 20 '15 at 20:11
  • @AndrejsIgumenovs _"which is probably too complicated"_ Not as _complicated_ as you might think (as mentioned, e.g. just run `UnitTester` under valgrind). Well, there are different tools for different purposes, and you are free, how to combine them to achieve what you actually want to analyze. – πάντα ῥεῖ Mar 20 '15 at 20:17
  • I think he was looking for something like `UnitTester --gtest.valgrind=true`, which is actually useful when one has like tens or hundreds of `UnitTester` executables that a continuous integration tool will run through. – HCSF Jun 16 '20 at 10:04
  • 1
    CppUTest detects leaks, however it's focused on embedded C/C++. – Technophile Dec 31 '20 at 19:05
  • @Technophile Well, did you see the date, when this answer was written? IIRC it didn't that time, and I had to run the testrunner executable under valgrind to do so. But you're right, that's not the focus of a unit test framework anyways. – πάντα ῥεῖ Jan 01 '21 at 12:19
  • @HCSF Could please guide or share more on, how to use the flags --gtest.valgrind=true ? – Prashant Adlinge Sep 27 '21 at 14:59
  • 1
    @PrashantAdlinge such flag doesn't exist. I was saying that the original poster was looking for a flag like `--gtest.valgrind` not looking for running gtest on top of valgrind. – HCSF Sep 28 '21 at 03:19
2

Not sure whether this worked in 2015, but since 2018 or so we use GoogleTest with CLang's sanitizers, including LeakSanitizer, AddressSanitizer and UndefinedBehavior sanitizer.

Just build tests with sanitizers enabled, example for the CMake-based project:

  add_compile_options(-fsanitize=leak,address,undefined -fno-omit-frame-pointer -fno-common -O1)
  link_libraries(-fsanitize=leak,address,undefined)
Nickolay Olshevsky
  • 13,706
  • 1
  • 34
  • 48
-1

Memory leaks are a result of incorrect use of system interfaces, The unit test should check if those interfaces are being used correctly in your unit under test, not what the implementation specific results of any of those interfaces is. It should check that the memory allocation and deallocation interfaces used directly by your unit are being used as designed. Testing the system specific results would be a part of component or integration testing. In the unit test, the memory management interfaces are external to the unit under test and thus should be stubbed out with a test implementation.

delfunc
  • 9
  • 5