Using _crtBreakAlloc to find memory leaks - identifier "_crtBreakAlloc" is unidentified
Asked Answered
J

6

11

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.

Jasmin answered 20/5, 2015 at 18:46 Comment(6)
Make sure you're using the debug version (/MTd or /MDd).Psychomancy
Where do I put those flags?Jasmin
Sounds like a coding problem. Please post the code. Otherwise we can only make WAGs.Ciborium
@Sunspawn: Unless you've changed the project properties (C/C++ -> Code Generation), just use the Debug and not Release configuration. Also make sure to do this: If you are using the multithreaded DLL version of the CRT library (the /MD option), include the context operator: {,,msvcr100d.dll}_crtBreakAlloc (for VS2013 it's msvcr120d).Psychomancy
I am using the default F5 debug mode. Is that something different?Jasmin
Okay, apparently I WAS using the /MD one - only now it tells me that "module msvcr100d.dll is not found" - despite it being right there in the System32 folder.Jasmin
J
4

_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;
Jardine answered 12/4, 2017 at 8:23 Comment(0)
T
9

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

Thyestes answered 16/1, 2019 at 12:41 Comment(1)
Had same issue! Note, this applies to e.g. VS 2019 where you use {,,ucrtbased.dll}_crtBreakAlloc as other answer suggests. Likely this page could be used to guess whatever symbol name should be used in future versions - Visual Studio documentationDetriment
J
4

_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;
Jardine answered 12/4, 2017 at 8:23 Comment(0)
F
4

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();
}
Foreshank answered 18/8, 2019 at 18:18 Comment(0)
C
3
{,,ucrtbased.dll}*__p__crtBreakAlloc()

works for Visual Studio 2017

Cerell answered 29/12, 2018 at 20:54 Comment(0)
S
2

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

Supporter answered 10/2, 2017 at 23:28 Comment(0)
G
0

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

(int*){,,ucrtbased.dll}__crtBreakAlloc
Gigantic answered 24/2, 2017 at 7:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.