Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using _crtBreakAlloc to find memory leaks - identifier "_crtBreakAlloc" is unidentified

I am trying to use _crtBreakAlloc in the Watch window as suggested in this link, but the value line says that 'identifier "_crtBreakAlloc" is unidentified' and it simply does not work.

What am I doing wrong? I'm using Visual Studio by the way.

An example of code:

#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>

#include <malloc.h>


int main()
{
    int *arr = (int*)malloc(10 * sizeof(int)); //breakpoint here
    free(arr);
    return 0;
}

I then write _crtBreakAlloc into the Name field of the Watch window and hit enter when encountering the breakpoint.

like image 550
Sunspawn Avatar asked May 20 '15 18:05

Sunspawn


People also ask

How do you detect memory leaks?

The primary tools for detecting memory leaks are the C/C++ debugger and the C Run-time Library (CRT) debug heap functions. The #define statement maps a base version of the CRT heap functions to the corresponding debug version. If you leave out the #define statement, the memory leak dump will be less detailed.

How do I detect memory leaks in Visual Studio?

To find memory leaks and inefficient memory usage, you can use tools such as the debugger-integrated Memory Usage diagnostic tool or tools in the Performance Profiler such as the . NET Object Allocation tool and the post-mortem Memory Usage tool.

What causes memory leaks in C++?

Memory leaks occur when new memory is allocated dynamically and never deallocated. In C programs, new memory is allocated by the malloc or calloc functions, and deallocated by the free function.

How do you handle memory leaks in C++?

The best way to avoid memory leaks in C++ is to have as few new/delete calls at the program level as possible – ideally NONE. Anything that requires dynamic memory should be buried inside an RAII object that releases the memory when it goes out of scope.


1 Answers

_crtBreakAlloc will be reported as unidentified if the ucrtbased.dll symbols are not loaded. I had this problem because I do not automatically load my symbols. You can go in your module list and manually Load symbols for ucrtbased.dll and then _crtBreakAlloc should show up and work.

like image 191
Claude Peloquin Avatar answered Sep 22 '22 12:09

Claude Peloquin