I just ran a test of the VS 2005 beta 2 debug CRT libraryThis is by design (and is almost the same behavior under all previous version of VC).
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_EVERY_1024_DF | _CRTDBG_CHECK_CRT_DF);
char * Test = new char[3];
Test[3] = '\0'; // Cause memory corruption
delete Test;
This results in a dialog box being shown which says that a heap overwrite occured with allocation X at location Y. No really useful information such as the filename/line number where the allocation occurred, or the number of bytes allocated.
If I jump into the debugger at that point, I see that the message was generated by line 1171 of dbgheap.c, and that it was generated from a _CrtMemBlockHeader structure which includes all of the useful info that I has hoped would be displayed.
So my question to the VC++ RTL guys is: you have the file/line info right there, why the heck don't you include it in the message
"malloc" is a define; here the __LINE__ and __FILE__ macro works correctly.
"new" is a function, therefor the __LINE__ and __FILE__ macro does not give you the right values... (or always the same).
For this you need an 3rd Party leak-finder (currently I am redisigning my leakfinder at http://blog.kalmbachnet.de/ postid=13)
-- Greetings Jochen Kalmbach Microsoft MVP VC++ My blog about Win32 and .NET http://blog.kalmbachnet.de/ PS: Please mark an answer as "answered" if it helped!!!

VS2005 debug CRT does not report file/line info for heap corruption
getrajnish
What info do you expect from the CRT in this situation
The only think the CRT can do is to say "Hey!" It has no context info who has written to the gap-place!
(Or maybe I misunderstand your question again...)
emz chisnky
#define new DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
- and you will get file & line info for new as well as malloc and free. Of course it only works with the debug CRT. The only real downside to it is that you must undefine new before including any of the STL headers that use the placement new syntax. This is pretty stupid, IMHO, since it would be so simple for the STL headers to handle this themselves (and yes I know they are outsourced to Dinkumware, but still).
So the info is there, why doesn't the CRT utilize it
Krams
_CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_WNDW);
If you add this, then it will display the file/line info in a messagebox, after you hit "Ignore" to the first one.
tnec
All MS has to do is copy the same logic they have in _CrtCheckMemory() into free_dbg_nolock().
thunderbird
#include <stdlib.h>
#include <crtdbg.h>
int main()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_EVERY_1024_DF | _CRTDBG_CHECK_CRT_DF);
char * Test = new char[3];
Test[3] = '\0'; // Cause memory corruption
delete Test;
return 0;
}
This produces a window with the following content:
"Debug Error .... Heap corruption error" (no line/file info).
Now we remove the "delete Test" and repalce this with:
for(int i=0; i<2000; i++)
{
char *c = (char*) malloc(10); free(c);
}
Now we get an error message like:
"Debug assertion failed! _CrtCheckMemory" (with useless file/line info: dbgheap.c:358)
You are correct in the fact, that there is an inconsistency between the two message-boxes.
But: What is the different to the developer Both dialogs only tells them that there is a memory corruption... not more and not less... the provided line/file info is completly useless...
lichh_2003
But where can you see some usefull infos in the "pHead" variable
&#42;Lysander&#42;
#define new DEBUG_NEW
#define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
- after including crtdbg.h. Then you get very useful file/line info. Try it and you will see.
mediaman
_aaa_
alazela
restore
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_DELAY_FREE_MEM_DF | _CRTDBG_CHECK_EVERY_1024_DF | _CRTDBG_CHECK_CRT_DF);
char * Test = new char[3];
Test[3] = '\0'; // Cause memory corruption
delete Test;
This results in a dialog box being shown which says that a heap overwrite occured with allocation X at location Y. No really useful information such as the filename/line number where the allocation occurred, or the number of bytes allocated.
If I jump into the debugger at that point, I see that the message was generated by line 1171 of dbgheap.c, and that it was generated from a _CrtMemBlockHeader structure which includes all of the useful info that I has hoped would be displayed.
So my question to the VC++ RTL guys is: you have the file/line info right there, why the heck don't you include it in the message
AdLearning.Net