添加一个函数调用时应用程序Phantom崩溃

时间:2011-06-03 15:50:59

标签: c++ visual-studio visual-studio-2008 crash first-chance-exception

我的应用程序有一些专门编写的调试代码,用于在调试模式下运行时将调试数据发送到输出窗口。当在下面的代码段中调用函数GetCurrTime时,当我单步执行代码时,或者在调用malloc之前的行上,应用程序在对malloc的以下调用时崩溃,如果我让它自由运行。然而,真正奇怪的是,当发生崩溃时,PC不会落在这两条线上。 PC在返回线上以完全不相关的功能停止。它变得更好了。调用堆栈显示函数无法返回的位置。我猜不知道PC正在走向杂草。是什么让这一切都很奇怪,就是当我注释掉GetCurrTime的电话时,问题就会消失。

void PrintDevMsgTrace( LPBYTE pMsg, PWCHAR fnName )
{
#ifdef _DEBUG
    BYTE byMsgLen;
    TCHAR * ptTimeStr = NULL;
    WORD cmd;
    int i, j = 0;
    int iTimeStrLen, iStrLen, iPreOffset, iPostOffset;
    wchar_t * pCmdIdStr = NULL;
    wchar_t * pBuf = NULL;

    byMsgLen = pMsg[DEV_LEN_OFFSET] + sizeof(devPktHead_t) + sizeof(devPktTail_t);
    cmd = pMsg[DEV_CMD_MSB_OFFSET];
    cmd <<= 8;
    cmd |= pMsg[DEV_CMD_LSB_OFFSET];
    pCmdIdStr = GetCmdIdStr( cmd );
    ptTimeStr = GetCurrTime();
    iTimeStrLen = ::wcsnlen_s( ptTimeStr, 128 );
    iPreOffset =
        iTimeStrLen                             // time string
        + 1                                     // "-"
        + ::wcsnlen_s( fnName, 128 )            // function name
        + 3                                     // " : "
        + ::wcsnlen_s( pCmdIdStr, 128 )         // command ID string
        + 3;                                    // " 0x"
    iPostOffset = iPreOffset + byMsgLen * 3;    // "%.2X " (formatted: 2 hex-nibble bytes and space)
    iStrLen = iPostOffset + 3;                  // "\r\n\0"
    pBuf = (wchar_t *)::malloc( iStrLen * sizeof(wchar_t) );

    ::swprintf_s( pBuf, iStrLen, _T("%s-%s : %s 0x"), ptTimeStr, fnName, pCmdIdStr);

    for ( i = iPreOffset; i < iPostOffset; i += 3 )
    {
        ::swprintf_s( &(pBuf[i]), 4, _T("%.2X "), pMsg[j++] );
    }

    ::swprintf_s( &(pBuf[i]), 3, _T("\r\n") );

    TRACE(pBuf);

    ::free( pBuf );
#endif
}

TCHAR * GetCurrTime( void )
{
    DWORD dwError = ERROR_SUCCESS;
    TCHAR * ptRetVal = NULL;
#ifdef _DEBUG
    int iTimeStrLen;

    do
    {
        if ( (iTimeStrLen = ::GetTimeFormat( LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, NULL, 0 )) == 0 )
        {
            dwError = ::GetLastError();
            TRACE(_T("%s : Failed getting time format.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        if ( (ptRetVal = (TCHAR *)::malloc( iTimeStrLen )) == NULL )
        {
            dwError = ERROR_NOT_ENOUGH_MEMORY;
            TRACE(_T("%s : Not enough memory.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        if ( ::GetTimeFormat( LOCALE_SYSTEM_DEFAULT, 0, NULL, NULL, ptRetVal, iTimeStrLen ) == 0 )
        {
            dwError = ::GetLastError();
            TRACE(_T("%s : Failed getting time format.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), _T(__FUNCTION__), dwError, _T(__FILE__), __LINE__);
            continue;
        }
    }
    while ( 0 );
#endif

    if ( dwError != ERROR_SUCCESS )
    {
        ::free( ptRetVal );
        ptRetVal = NULL;
    }

    ::SetLastError( dwError );

    return ptRetVal;
}

只是为了踢,这是崩溃发生时PC进入的功能(在函数最后一行的return语句中):

LPVOID CLinkList::Add( LPVOID pItem, DWORD len )
{
    DWORD dwError = ERROR_SUCCESS;
    LPVOID pItemCopy = NULL;
    LPLIST_NODE_T ptNode = NULL;

    do
    {
        // Validate parameters.
        if ( (pItem == NULL) || (len == 0) )
        {
            dwError = ERROR_INVALID_PARAMETER;
            TRACE(_T("CLinkList::Add : Invalid parameter.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        if ( this->m_blCopy == FALSE )
        {
            pItemCopy = pItem;
        }
        else if ( (pItemCopy = ::malloc( len )) == NULL )
        {
            dwError = ERROR_NOT_ENOUGH_MEMORY;
            TRACE(_T("CLinkList::Add : Failed to allocate memory.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }
        else
        {
            ::memcpy( pItemCopy, pItem, len );
        }

        if ( (ptNode = (LPLIST_NODE_T)::malloc( sizeof(LIST_NODE_T) )) == NULL )
        {
            dwError = ERROR_NOT_ENOUGH_MEMORY;
            TRACE(_T("CLinkList::Add : Failed to allocate memory.\r\n\tError: %d\r\n\tFile: %s\r\n\tLine: %d\r\n"), dwError, _T(__FILE__), __LINE__);
            continue;
        }

        ptNode->next = NULL;
        ptNode->item = pItemCopy;
        ptNode->len = len;

        if ( this->m_ptFirstNode == NULL )
        {
            ptNode->prev = NULL;
            this->m_ptFirstNode = ptNode;
        }
        else
        {
            ASSERT(this->m_ptLastNode != NULL);

            ptNode->prev = this->m_ptLastNode;
            this->m_ptLastNode->next = ptNode;
        }

        this->m_ptLastNode = ptNode;
        this->m_dwItemCount++;
    }
    while ( 0 );

    if ( dwError != ERROR_SUCCESS )
    {
        ::free( ptNode );

        if ( this->m_blCopy != FALSE )
        {
            ::free( pItemCopy );
        }

        pItemCopy = NULL;
    }

    ::SetLastError( dwError );

    return pItemCopy;
}

这是输出窗口中打印的错误:

  

0x7c936822处的第一次机会异常   在ZCT.exe中:0xC0000005:访问   违规读取位置0x00000000。   HEAP [ZCT.exe]:Heap缺少最后一个条目   在5451460附近的承诺范围内   Windows触发了一个断点   ZCT.exe。

     

这可能是由于腐败造成的   堆,表示ZCT.exe中的错误   或者它加载的任何DLL。

     

这也可能是由于用户造成的   在ZCT.exe具有焦点时按F12。

     

输出窗口可能有更多   诊断信息。该程序   '[0x9F4] ZCT.exe:Native'退出了   代码为0(0x0)。

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

ptRetVal = (TCHAR *)::malloc( iTimeStrLen )

当您可能想要分配wchar_t s的数量时,会分配多个字节。