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.

Sunspawn
  • 817
  • 1
  • 12
  • 27
  • Make sure you're using the debug version (`/MTd` or `/MDd`). – cremno May 20 '15 at 19:31
  • Where do I put those flags? – Sunspawn May 20 '15 at 19:35
  • Sounds like a coding problem. Please post the code. Otherwise we can only make WAGs. – user3629249 May 20 '15 at 19:39
  • @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`). – cremno May 20 '15 at 19:55
  • I am using the default F5 debug mode. Is that something different? – Sunspawn May 20 '15 at 20:08
  • 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. – Sunspawn May 21 '15 at 10:42

6 Answers6

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.

  • 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 documentation](https://learn.microsoft.com/en-us/visualstudio/debugger/finding-memory-leaks-using-the-crt-library?view=vs-2019) – stoper Oct 31 '20 at 16:26
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;
Carl
  • 139
  • 7
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();
}
3
{,,ucrtbased.dll}*__p__crtBreakAlloc()

works for Visual Studio 2017

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

Malick
  • 6,252
  • 2
  • 46
  • 59
0

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

(int*){,,ucrtbased.dll}__crtBreakAlloc
Luke Dunstan
  • 4,862
  • 1
  • 17
  • 15