Posts

Showing posts with the label Debugging

memory leak detection notes (continuously updating)

Memory leak problem is very common, below is some my previous working notes: BoundsCheck BoundsCheck does not work well with mixed codes. Since my latest projects are almost all mixed, so, seldom used in my daily work. Visual studio Enable memory leak reporting There are two ways to enable memory leak check in VC, one is to define DEBUG_NEW macro, another is as below: 1: #define _CRTDBG_MAP_ALLOC 2: #include<stdlib.h> 3: #include<crtdbg.h> Then calling _CrtSetDbgFlag ( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF ); when initializing your app. With the above changes, visual studio can dump memory block leak when quitting or between two snapshot via Windows API. For instance: c:\program files(x86)\microsoft visual studio\vc98\include\crtdbg.h(552) : {43} normal block at 0x00441C20, 40 bytes long. Data: < C > 08 02 43 00 16 00 00 00 00 00 00 00 00 00 00 00 Output mode The above output is one sample of block leak, in which, {43} is the heap block a...

BoundsCheck not working with mixed codes

Previously, we have tried to use BoundsChecker to test our mixed programs. But it does not support managed code and also cause huge memory usage during running, Recently, I tried "Application Verifier", a great tool distributed by Microsoft, can be used alone or together with Visual studio team version, and it seems to be acceptable. The below code can be found immediately: 1: int * p = new int [10]; 2: p[11]=1; // this cannot be detected, since the heap block has additional 8-bytes suffix, it seems that AppVerif does not check with this 3: p[100] = 1; // this can be detected immediately, since AppVerif generates memory fence after the heap block. After running with Application Verifier, the memory usage is also acceptable. Another thing related to this is that AppVerif can only work with native codes. As for the mixed code, msdn does not say clearly. Two potential results can be: 1: AppVerif can only check native memory, ignore managed objects. 2: AppV...

Setting data breakpoints for mixed projects in VS2008

In VS2008, data breakpoints are disabled in GUI for mixed projects, which is actually a powerful debugging mechanism for trouble-shooting. Although it could still be set in windbg, still painful. Fortunately, data breakpoints can be set programmatically. We can found one sample from below link: http://www.morearty.com/code/breakpoint/ Then, in your projects, try below code:   1: DWORD x = 1; 2: CBreakpoint bp; 3: bp.Set(&x, sizeof (x), CBreakpoint::Write); 4: ... 5: x = 100; 6:   Generally, I would like to create an foo debugging thread, keep looping/sleeping, if needed, explicitly set the watch target dynamically in the debugger, then it works well.