【问题标题】:Visual C++ Debug Assertion FailVisual C++ 调试断言失败
【发布时间】:2016-02-19 12:28:36
【问题描述】:

当我编译我的程序时,它会说“调试断言失败”。
当我点击忽略两次时,我的应用程序就会出现。
我该如何解决?

如果您想自己测试一下,整个代码如下。应用程序的数量随着滚动条向右移动而增加,随着滚动条向左移动而减少。但我的主要问题是调试断言失败。如果可以,请发布有问题的代码部分以及我如何修复它或修复它并发布代码。有什么方法可以在不使用 try and catch 的情况下摆脱运行时错误。我的意思是解决这个问题。这是一个项目,我只有一个半星期的时间去做。如果你能帮助我完成这个项目,你的帮助将不胜感激。我对视觉 C++ 相当陌生。部分错误是:

Program E:\BHCCHardwareStore\Debug\BHCCHardwareStore.exe 文件:f\dd\vctools\vc7libs\ship\atlmfc\src\mfc\appcore.cpp 行:196。有关您的程序如何导致断言失败的信息请参阅有关断言的 Visual C++ 文档。 (按重试调试应用程序)。

现在,当我单击忽略时,它会完美地显示应用程序。该应用程序似乎运行良好。如何修复此错误此代码不是我正在剪切和粘贴的整个代码?

#include "stdafx.h"
#include <strstream>
#include <afxwin.h>
#include <string.h>
const int IDC_SB1 = 100;
const int IDC_CS1 = 101;
const int IDC_CS2 = 102;
const int IDC_BUTTON = 103;

const int MIN_RANGE = 0;
const int MAX_RANGE = 100;
class CApp :public CWinApp
{
public:
    virtual BOOL InitInstance();
};

CApp App;

class CSource :public CFrameWnd
{
    CScrollBar*sb1;
    CStatic* cs1;
    CStatic* cs2;
    CButton* button;
public:
    CSource();
    afx_msg void OnHScroll(UINT nSBCode,
        UINT nPos, CScrollBar* pSccrollBar);
    afx_msg void handleButton();
    DECLARE_MESSAGE_MAP();
};

BEGIN_MESSAGE_MAP(CSource, CFrameWnd)
    ON_WM_HSCROLL()
    ON_BN_CLICKED(IDC_BUTTON, handleButton)
END_MESSAGE_MAP()

//Window cONSTRUCTOR

CSource::CSource()
{

}

void CSource::OnHScroll(UINT nSBCode,
    UINT nPos, CScrollBar* pScrollBar)
{
    int pos, dividend = 0, holder = 0, x = 0;
    char array[9];
    //;
    pos = pScrollBar->GetScrollPos();
    switch (nSBCode)
    {
    case SB_LINEUP:
        pos -= 1;
        break;
    case SB_LINEDOWN:
        pos += 1;
        break;
    case SB_PAGEUP:
        pos -= 10;
        break;
    case SB_PAGEDOWN:
        pos += 10;
        break;
    case SB_TOP:
        pos = MIN_RANGE;
        break;
    case SB_BOTTOM:
        pos = MAX_RANGE;
        break;
    case SB_THUMBTRACK:
        pos = nPos;
        break;
    default:
        return;
    }

    if (pos < MIN_RANGE)
        pos = MIN_RANGE;
    else if (pos > MAX_RANGE)
        pos = MAX_RANGE;
    sb1->SetScrollPos(pos, TRUE);

    //Set the labels to the new values
    char s[100];
    TCHAR s1[100];
    std::ostrstream ostr(s, 100);
    ostr << "Decimal Value = " << pos << std::ends;
    for (int i = 0; i < 100; i++){
        s1[i] = (TCHAR) s[i];
    }
    SetDlgItemText(IDC_CS1, s1);
    ostr.seekp(std::ios::beg);

    dividend = pos;
    for (int y = 0; y < 9; y++)
        array[y] = '0';
    int remainder = dividend % 2;
    int quotient = dividend / 2;
    array[x] = (char)(remainder + 48);
    do
    {
        remainder = quotient % 2;
        quotient = quotient / 2;
        array[++x] = (char)(remainder + 48);
    } while (quotient != 0);
    array[8] = '\0';
    ostr << "Binary Value = " << _strrev(array) << std::ends;
    SetDlgItemText(IDC_CS2, s1);
}

void CSource::handleButton()
{
    int result;
    result = MessageBox(_T("Are you sure?"), _T("Exiting"),
        MB_ICONQUESTION | MB_YESNO);
    if (result == IDYES)
    {
        Beep(1000, 100);
        DestroyWindow();
    }
    else
        Beep(200, 100);
}

//Initialize the application and the main window
BOOL CApp::InitInstance()
{
    m_pMainWnd = new CSource();
    m_pMainWnd->ShowWindow(m_nCmdShow);
    m_pMainWnd->UpdateWindow();
    return TRUE;
}

【问题讨论】:

  • 您的CSource 类中有一堆成员指针,例如sb1,您在代码中使用它们。你在哪里初始化这些指针?我在你的代码中没有看到它。
  • 知道第 196 行是什么也很有帮助。
  • 不是单击“忽略”,而是“重试”或“调试”将您带到哪里?您应该看到一个调用堆栈——转到调用堆栈中它指向您的代码的条目。正是那条线导致了断言。

标签: c++ visual-c++ mfc


【解决方案1】:

您的代码(在 VS2013 中)在 MFC 代码中生成断言:

BOOL CWnd::ShowWindow(int nCmdShow)
{
    ASSERT(::IsWindow(m_hWnd) || (m_pCtrlSite != NULL));

来自您的电话:

m_pMainWnd->ShowWindow(m_nCmdShow);

您必须先创建您的窗口,然后才能显示它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多