tags:

views:

292

answers:

2

I'm using Visual Studio to debug an ATL application.

When I step over return TRUE in this code, the error occurs:

BOOL CMainFrame::OnCopyData(CWnd* pWnd, COPYDATASTRUCT* pCopyDataStruct) {

    // Code snipped from here - maybe this causes stack/heap corruption?

    // I have a breakpoint here, if I step over (F10), AFX trace message 
    // is shown (as below)
    return TRUE;

}

This is the message box that's shown:

Windows has triggered a breakpoint in foobar.exe.

This may be due to a corruption of the heap, which indicates a bug in foobar.exe or any of the DLLs it has loaded.

This may also be due to the user pressing F12 while phonejournal.exe has focus.

The output window may have more diagnostic information.

The message is a little vague, and I'm wondering what tools I can use to get more information. The debugger breaks on the call to AtlTraceVU in atltrace.h:

 inline void __cdecl CTrace::TraceV(const char *pszFileName, int nLine,
  DWORD_PTR dwCategory, UINT nLevel, LPCWSTR pszFmt, va_list args) const
 {
  AtlTraceVU(m_dwModule, pszFileName, nLine, dwCategory, nLevel, pszFmt, args);
 }
A: 

Your memory (perhaps your stack) is corrupted by a wayward pointer somewhere else in the code.

mlimber
Yup, but how do I find it? The code is massive. Maybe it has nothing to do with OnCopyData -- or maybe it's because I'm doing something bad to one of the args passed in (which I don't think is the case). What tools can I use to get the line of code that's causing the error?
nbolton
Unless you can wrap possible offenders in smart, checking, wrappers you'd probably have to resort to *divide and conquer* - disable some/half/... of the code, if it doesn't happen anymore you know where to look deeper.
Georg Fritzsche
+3  A: 

Microsoft's Application Verifier may help with this. If the application has heap corruption, this utility can cause the exception to occur when the error occurs. It can use a lot of memory when running, though, since it can produce big changes in memory allocation schemes.

The following obviously flawed code gives a simple demonstration:

char *pc = malloc( 4 );
memcpy( pc, "abcdabcd", 9 );
free( pc );

When I ran this without application verifier, it ran to completion with no obvious error. With application verifier, though, it caused an exception (0x80000003). Application Verifier forced the allocation to be at the end of a segment (e.g., 0x1e9eff8). The memcpy resulted in a write into the subsequent segment, which resulted in the exception during the memcpy call. If the overwrite is less in this simple example, the break doesn't occur until the free call, but that is still better than no exception. It's a pretty cool utility.

Mark Wilkins
Nice, thanks! I haven't tried this yet, but will accept this as answer when I do.
nbolton
Haven't tried this yet, but it turned out to be heap corruption (discovered it using trial and error) but I'll remember Application Verifier for next time.
nbolton