2

I had a memory leak in my big program, detected by the Visual Studio CRT debug system. I reduced my program to the following, with still shows a memory leak.

#include "stdafx.h"
#include "crtdbg.h"

int main()
{
    int tmp = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
    _CrtSetDbgFlag(tmp | _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); 
    int* k = new int(8);
    delete k;
    return 0;
}

When I run it in my Visual Studio 2012 system, I see the following:

Detected memory leaks!
Dumping objects ->
{65} normal block at 0x00663008, 4424 bytes long.
 Data: <X    #f         > 58 CF 14 00 90 23 66 00 01 00 00 00 00 00 00 00 
{64} normal block at 0x00662390, 4 bytes long.
 Data: <    > 00 C3 14 00 
Object dump complete.

If I remove the allocation and deallocation, the leaks don't appear. If I replace the allocation and deallocation by any standard library feature that uses memory allocation (e.g. std::string k), the leaks appear.

Why do the memory leaks appear? How can I remove them?


I tried debugging my problem by setting _crtBreakAlloc to 64; the system stopped at a place that is supposed to help me (see stack trace below). But I don't know what to do with this info.

>   test_it.exe!_heap_alloc_dbg_impl(unsigned int nSize, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 393   C++
    test_it.exe!_nh_malloc_dbg_impl(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine, int * errno_tmp) Line 239    C++
    test_it.exe!_nh_malloc_dbg(unsigned int nSize, int nhFlag, int nBlockUse, const char * szFileName, int nLine) Line 302  C++
    test_it.exe!malloc(unsigned int nSize) Line 56  C++
    test_it.exe!_PlatformSpecificMalloc()  Unknown
    test_it.exe!MemoryLeakWarningPlugin::ignoreAllLeaksInTest(void) Unknown
    test_it.exe!operator new(unsigned int)  Unknown
    test_it.exe!MemoryLeakWarningPlugin::getGlobalDetector(void)    Unknown
    test_it.exe!std::error_condition::value(void)   Unknown
    test_it.exe!operator new(unsigned int)  Unknown
    test_it.exe!main() Line 9   C++

My system is:

  • Microsoft Visual Studio Professional 2012
  • Version 11.0.61030.00 Update 4
  • Visual C++ 2012 04938-004-0034007-02224
  • Windows 7
anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • What happens if you just call `_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);` without combining them with the existing bits set? – sjdowling Jan 29 '15 at 09:51
  • @sjdowling No change – anatolyg Jan 29 '15 at 09:58
  • What runtime library are you linking to? If it is `Multithread Debug DLL`, change it to `Multithread Debug` and see if the problem still exists after rebuilding and rerunning your app.. – PaulMcKenzie Jan 29 '15 at 10:04
  • @PaulMcKenzie I am already using `Multi-threaded Debug (/MTd)` (copied this from the project's settings) – anatolyg Jan 29 '15 at 10:09
  • @anatolyg I tried your program with VS 2013, and can't duplicate your issue. There are no leaks reported with the code you have. To verify, I commented out the `delete k;` line, and the leak report appears. – PaulMcKenzie Jan 29 '15 at 10:17
  • 2
    @anatolyg - These functions in your call stack are, as far as I know, *not* Visual C++ runtime internal functions. Using google takes me here: https://github.com/auser/cpputest/blob/master/src/CppUTest/MemoryLeakWarningPlugin.cpp. I suggest you create a *brand new*, pristine, Win32 Console application and retest your code. – PaulMcKenzie Jan 29 '15 at 10:25
  • @PaulMcKenzie Yeah, this is the cause of my problem! My project uses a compiled CppUTest lib, with no source code. – anatolyg Jan 29 '15 at 10:29
  • @anatolyg - ok, I added my comment as an answer. – PaulMcKenzie Jan 29 '15 at 10:32

2 Answers2

3

Your call stack suggests that there is another memory leak tool being used, besides the Visual C++ runtime functions.

Using google takes me to this link: https://github.com/auser/cpputest/blob/master/src/CppUTest/MemoryLeakWarningPlugin.cpp

So possibly, cppuTest is being applied to your simple project without you being aware of it.

I suggest you create a brand new Win32 Console application, copy and paste your code, and retest. Make sure that the new project has no additional dependencies.

PaulMcKenzie
  • 34,698
  • 4
  • 24
  • 45
0

Just tried it with clean VS 2012 installation with Deleaker installed. No leaks shown. And no output from CRT in the final.

What is MemoryLeakWarningPlugin mentioned in the stack trace? Seems it's a part CppUTest (I did Google a bit).

I think either MemoryLeakWarningPlugin leaks itself or it breaks CRT diagnostic system somehow.

Artem Razin
  • 1,234
  • 8
  • 22