Our C# code is calling an unmanaged C++ DLL. I want to have the ability to catch all unmanaged memory leaks that may occur while we’re using the unmanaged code. May I call _CrtSetDbgFlag at the start of the C++ DLL’s initialization function and obtain meaningful results by calling _CrtDumpMemoryLeaks at the end of the shutdown function?
1
Yes, the pairing of _CrtSetDbgFlag and _CrtDumpMemoryLeaks calls across different functions within the DLL works. I added this line void* tests_memory_leak_reporting = malloc(100 * sizeof(byte));
right after the _CrtSetDbgFlag call and saw the output below generated from the function that called _CrtDumpMemoryLeaks.
Detected memory leaks!
Dumping objects ->
C:<ommitted>ApiFunc.cpp(327) : {72} normal block at 0x000001E972736C30, 100 bytes long.
Data: < > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
Don’t forget to use the section below in order to get source code information in the leaked objects’ dump.
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>