100% CPU usage and being stuck in Memory Leak Detection

91 views
Skip to first unread message

Kai Mast

unread,
Apr 8, 2016, 1:47:51 PM4/8/16
to cpputest
After running my tests CppUtest just gets stuck in the memory leak detector using 100% of one my CPU cores.

0    MemoryLeakDetectorList::isInPeriod(MemoryLeakDetectorNode *, MemLeakPeriod) [clone .part.0]            0x521060   
1    MemoryLeakDetectorList::getTotalLeaks(MemLeakPeriod)            0x5219b2   
2    MemoryLeakDetector::totalMemoryLeaks(MemLeakPeriod)            0x52271f   
3    MemoryLeakWarningPlugin::postTestAction(UtestShell&, TestResult&)            0x517368   
4    TestPlugin::runAllPostTestAction(UtestShell&, TestResult&)            0x51c3f7   
5    PlatformSpecificSetJmpImplementation            0x51f86a   
6    UtestShell::runOneTest(TestPlugin *, TestResult&)            0x51da4a   
7    TestRegistry::runAllTests(TestResult&)            0x51d009   
8    CommandLineTestRunner::runAllTests()            0x514a30   
9    CommandLineTestRunner::runAllTestsMain()            0x514b1d   
10    CommandLineTestRunner::RunAllTests(int, const char * *)            0x514c6d   
11    main    main.cpp    16    0x4b8363   

Any Idea what to do? How to debug this?

Kai Mast

unread,
Apr 8, 2016, 3:06:16 PM4/8/16
to cpputest
I think this catches the problem. I could probably debug this myself, but the code has absolutely no comments....

--- a/src/CppUTest/MemoryLeakDetector.cpp
+++ b/src/CppUTest/MemoryLeakDetector.cpp
@@ -30,6 +30,8 @@
 #include "CppUTest/PlatformSpecificFunctions.h"
 #include "CppUTest/SimpleMutex.h"
 
+#include <assert.h>
+
 #define UNKNOWN ((char*)("<unknown>"))
 
 SimpleStringBuffer::SimpleStringBuffer() :
@@ -351,6 +353,7 @@ int MemoryLeakDetectorList::getTotalLeaks(MemLeakPeriod period)
 {
     int total_leaks = 0;
     for (MemoryLeakDetectorNode* node = head_; node; node = node->next_) {
+        assert(node != node->next_);
         if (isInPeriod(node, period)) total_leaks++;
     }
     return total_leaks;

Steven Collins

unread,
Apr 8, 2016, 3:25:31 PM4/8/16
to cppu...@googlegroups.com
This is just a guess, based on your problem description. I would hazard that the linked list, which is wrapped in some way around the memory returned to your code from the allocation routine has gotten corrupted. I'd start by checking all the allocations to make sure they are for the correct data type. Is your code C or C++? It is probably easier to mis-allocate storage in C.

The other possibility would seem to be an issue in the leak detector itself, creating a circular linked list, but I find this less likely since you're the only one reporting the issue.

Walking into the stuck code with a debugger might help you better classify the issue.

--
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.
For more options, visit https://groups.google.com/d/optout.

Bas Vodde

unread,
Apr 9, 2016, 2:47:23 AM4/9/16
to cppu...@googlegroups.com

Hi Kai,

Just curious, do you seriously use comments to understand how code works? :)

You’d be one of the first I ever met ;)

—————-

Hope this is resolved. As others suggested, the memory leak code is rather old and haven’t had much bug reports for a long time. Most commonly problems happen because of memory corruption.

I’d recomment to gradually reduce your test to find the exact line that this is caused. A technique that might help with this:

Bas

Message has been deleted

James Grenning

unread,
Apr 11, 2016, 6:35:26 PM4/11/16
to cpputest
Hello Kai

What test is running when it gets into the infinite loop?

Maybe you tried this, but here is what I would do, collect information:

Run the tests with -v verbose. See which test is getting stuck.

Once you know the test, use the -g and -n switches to select the test
group, or the test name to run the test in isolation.

You might try other tests in the same group and make sure it is just the
one test. If it is more than one test, that is information to help you
debug this. When you get to one test that has the infinite loop, can
you cut the example down to try to establish cause and effect.

Did the infinite loop happen with some change you made (while test
driving) or were these tests added after development? If via TDD, back
out the change. Again cause and effect.

I C and C++ if your code goes out of bounds of where it is supposed to
go, that is undefined behavior. Given the couple emails I read, sound
like your code has run off and clobbered something, hence the endless
loop. A problem like this can exist for years in code that appear to be
working fine.

I saw your comment about no comments. We have something better, tests
:-). You have a repeatable problem! BTW: that code is walking down a
linked list. I suspect the production code damages some link in the
list.

To confirm you are in that loop, I suggest you change your assert to
look at total_leaks. Make it some number larger that the number of
allocations you would expect (by a factor of 2) like this
assert(total_leaks < 200); The assert you have will terminate way too
soon, like on the first leak when there is more that one and it is not
the first leak. Doesn't that stack trace suggest the code was in
"isInPeriod()"?

Good luck, but more importantly, good skill!

James


--------------------------------------------------------------------------
James Grenning - Author of TDD for Embedded C - wingman-sw.com/tddec
wingman-sw.com
wingman-sw.com/blog
twitter.com/jwgrenning
facebook.com/wingman.sw
[![wingman
software](http://www.wingman-sw.com/images/wingman.png)](http://wingman-sw.com)

James Grenning

unread,
Apr 13, 2016, 7:03:55 PM4/13/16
to cpputest
Kai

I hope you find the problem where ever it comes from. Let us know how
we can help. Keep us posted.

James
Reply all
Reply to author
Forward
0 new messages