Sunspawn
Sunspawn

Reputation: 837

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.

Upvotes: 12

Views: 5732

Answers (6)

user11943493
user11943493

Reputation:

few options:
- add {,,ucrtbased.dll }_crtBreakAlloc as variable to Watch
this requires symbols loaded in order watch window to display variable type correctly

find out which CRT version crt*.dll you compile against. (new ucrtbased.dll, older msvcrtd*.dll, etc)
https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=vs-2019
load all modules, or add manually into \tools\options\debug\symbols\load only spec modules
note: your Debug config compile with /MDd (it will have _DEBUG defined)
Release config compile with /MD (most of debug macros just zero;)


- use memchk_break macro as bellow, it will show alloc block in locals automatically
(since it resolves through compilation)


- let it run, pass on first break, let it print mem leaks if any
- on second round, type in the alloc block into variable, run and catch


#ifdef _DEBUG
   #define memchk_break() { auto& _ab = _crtBreakAlloc; __debugbreak(); }
#else
   #define memchk_break() 0;
#endif
void main(){
   memchk_break();
   // your code
   _CrtDumpMemoryLeaks();
}

Upvotes: 4

Claude Peloquin
Claude Peloquin

Reputation: 91

_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.

Upvotes: 9

Rafał Skorka
Rafał Skorka

Reputation: 43

{,,ucrtbased.dll}*__p__crtBreakAlloc()

works for Visual Studio 2017

Upvotes: 3

Carl
Carl

Reputation: 133

_crtBreakAlloc is a macro under VS2015 that is replaced by a call to a function returning a pointer to an int. Tracking a variable in the watch window doesn't seem an option.
Better insert in your (debug) code something like this:

_crtBreakAlloc = 18;

Upvotes: 4

Luke Dunstan
Luke Dunstan

Reputation: 4882

It seems that for Visual Studio 2015 it is necessary to use two underscores:

(int*){,,ucrtbased.dll}__crtBreakAlloc

Upvotes: 0

Malick
Malick

Reputation: 6732

If you are using the multithread version of the CRT, enter the following in the watch window (in column name) :

(int*){,,ucrtbased.dll}_crtBreakAlloc

then press enter and change the value -1 to the new allocation number that causes a user-defined breakpoint.

enter image description here

Upvotes: 2

Related Questions