ProbablyAStupidQuestion ProbablyAStupidQuestion - 3 years ago 42
C++ Question

CRT Doesn't print line number of memory leak

I've got the code below, which I think, based on this https://msdn.microsoft.com/en-us/library/x98tx3cf.aspx should print out the line number of a memory leak.

#include "stdafx.h"
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
#include <iostream>


void derp()
{
int* q = new int;

}

int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
derp();
return 0;
}


When I run it, I get the following:

Detected memory leaks!
Dumping objects ->
{75} normal block at 0x0067E930, 4 bytes long.
Data: < > CD CD CD CD
Object dump complete.


Based on Microsoft's documentation, I would expect the line where the leaky memory was allocated to print, but indeed it does not. Do you all see the same? What have I done wrong? I'm using VS2015.

Answer Source

Check out the answer here. You want to use overloaded new operator with the additional parameters as specified in the solution there in order to get that information.

In this case, change your line

int* q = new int;

to

int* q = new (_NORMAL_BLOCK, __FILE__, __LINE__) int;

and you should see the leaks.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download