Using _crtBreakAlloc to find memory leaks - identifier “_crtBreakAlloc” is unidentified

≯℡__Kan透↙ 提交于 2019-12-23 08:28:06

问题


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.


回答1:


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



回答2:


{,,ucrtbased.dll}*__p__crtBreakAlloc()

works for Visual Studio 2017




回答3:


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




回答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://docs.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();
}



回答5:


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.




回答6:


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

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


来源:https://stackoverflow.com/questions/30357986/using-crtbreakalloc-to-find-memory-leaks-identifier-crtbreakalloc-is-unide

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!