viewrich.cpp中的Debug Assertion Error

时间:2013-12-18 03:25:34

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

我正在VS2010中创建一个简单的MFC文本编辑器,以便学习C ++,而且我遇到了一些绊脚石....

即使一切都编译完毕。当我在调试模式下运行程序时,它在viewrich.cpp的151,156和1875行给出了一个“Debug Assertion Failed”错误,它是VS2010的MFC的一部分。

第151和156行是以下代码之一:

int CRichEditView::OnCreate(LPCREATESTRUCT lpcs)
{
    if (CCtrlView::OnCreate(lpcs) != 0)
        return -1;
    GetRichEditCtrl().LimitText(lMaxSize);
    GetRichEditCtrl().SetEventMask(ENM_SELCHANGE | ENM_CHANGE | ENM_SCROLL);
    VERIFY(GetRichEditCtrl().SetOLECallback(&m_xRichEditOleCallback)); //LINE 151
    m_lpRichEditOle = GetRichEditCtrl().GetIRichEditOle();
    DragAcceptFiles();
    GetRichEditCtrl().SetOptions(ECOOP_OR, ECO_AUTOWORDSELECTION);
    WrapChanged();
    ASSERT(m_lpRichEditOle != NULL); //LINE 156
    return 0;
}

第1875行是以下代码之一:

void CRichEditDoc::SetTitle(LPCTSTR lpszTitle)
{
    COleServerDoc::SetTitle(lpszTitle);
    CRichEditView *pView = GetView();
    ASSERT(pView != NULL);
    ASSERT(pView->m_lpRichEditOle != NULL); // LINE 1875
    CStringA strAppName(AfxGetAppName()), strTitle(lpszTitle);
    pView->m_lpRichEditOle->SetHostNames(strAppName.GetString(), 
        strTitle.GetString());
}

以下是相关文件:

cntritem.h:

#include "afxrich.h"
// #include "Resource.h"
class CEmergenceDoc;
class CEmergenceView;

class CEmergenceCntrItem : public CRichEditCntrItem
{
    DECLARE_SERIAL(CEmergenceCntrItem)

// Constructors
public:
    CEmergenceCntrItem(REOBJECT* preo = NULL, CEmergenceDoc* pContainer = NULL);
        // Note: pContainer is allowed to be NULL to enable      
                IMPLEMENT_SERIALIZE.
        //  IMPLEMENT_SERIALIZE requires the class have a constructor with
        //  zero arguments.  Normally, OLE items are constructed with a
        //  non-NULL document pointer.

// Attributes
public:
    CEmergenceDoc* GetDocument()
        { return (CEmergenceDoc*)COleClientItem::GetDocument(); }
    CEmergenceView* GetActiveView()
        { return (CEmergenceView*)COleClientItem::GetActiveView(); }

    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CWordPadCntrItem)
    public:
    protected:
    //}}AFX_VIRTUAL

// Implementation
public:
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif
};

EmergenceView.cpp

#include "stdafx.h"
// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail
// and search filter handlers and allows sharing of document code with that project.
#ifndef SHARED_HANDLERS
#include "Emergence.h"
#endif
#include "EmergenceDoc.h"
#include "EmergenceView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CEmergenceView

IMPLEMENT_DYNCREATE(CEmergenceView, CRichEditView)

BEGIN_MESSAGE_MAP(CEmergenceView, CRichEditView)
    // Standard printing commands
    ON_COMMAND(ID_FILE_PRINT, &CRichEditView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_DIRECT, &CRichEditView::OnFilePrint)
    ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CEmergenceView::OnFilePrintPreview)
    ON_WM_CONTEXTMENU()
    ON_WM_RBUTTONUP()
END_MESSAGE_MAP()

// CEmergenceView construction/destruction

CEmergenceView::CEmergenceView()
{
    // TODO: add construction code here

}

CEmergenceView::~CEmergenceView()
{
}

BOOL CEmergenceView::PreCreateWindow(CREATESTRUCT& cs)
{
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    return CView::PreCreateWindow(cs);
}

// CEmergenceView drawing

void CEmergenceView::OnDraw(CDC* /*pDC*/)
{
    CEmergenceDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    if (!pDoc)
        return;

    // TODO: add draw code for native data here
}


// CEmergenceView printing


void CEmergenceView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
    AFXPrintPreview(this);
#endif
}

BOOL CEmergenceView::OnPreparePrinting(CPrintInfo* pInfo)
{
    // default preparation
    return DoPreparePrinting(pInfo);
}

void CEmergenceView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add extra initialization before printing
}

void CEmergenceView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
    // TODO: add cleanup after printing
}

void CEmergenceView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
    ClientToScreen(&point);
    OnContextMenu(this, point);
}

void CEmergenceView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
    theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y,     

this, TRUE);
#endif
}

// CEmergenceView diagnostics

#ifdef _DEBUG
void CEmergenceView::AssertValid() const
{
    CView::AssertValid();
}

void CEmergenceView::Dump(CDumpContext& dc) const
{
    CView::Dump(dc);
}

CEmergenceDoc* CEmergenceView::GetDocument() const // non-debug version is inline
{
    ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEmergenceDoc)));
    return (CEmergenceDoc*)m_pDocument;
}
#endif //_DEBUG


// CEmergenceView message handlers

EmergenceDoc.cpp

#include "stdafx.h"
#include "Emergence.h"

#include "EmergenceDoc.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

extern BOOL AFXAPI AfxFullPath(LPTSTR lpszPathOut, LPCTSTR lpszFileIn);
extern UINT AFXAPI AfxGetFileTitle(LPCTSTR lpszPathName, LPTSTR lpszTitle, UINT nMax);

#ifndef OFN_EXPLORER
#define OFN_EXPLORER 0x00080000L
#endif

#include "cntritem.h"

IMPLEMENT_DYNCREATE(CEmergenceDoc, CRichEditDoc)

BEGIN_MESSAGE_MAP(CEmergenceDoc, CRichEditDoc)
    ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, 
        CRichEditDoc::OnUpdateEditLinksMenu)
    ON_COMMAND(ID_OLE_EDIT_LINKS, CRichEditDoc::OnEditLinks)
    ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, 
        ID_OLE_VERB_LAST, CRichEditDoc::OnUpdateObjectVerbMenu)
END_MESSAGE_MAP()

CEmergenceDoc::CEmergenceDoc()
{
}

CEmergenceDoc::~CEmergenceDoc()
{
}

BOOL CEmergenceDoc::OnNewDocument()
{
    if (!CRichEditDoc::OnNewDocument())
        return FALSE;
    return TRUE;
}

CRichEditCntrItem* CEmergenceDoc::CreateClientItem(REOBJECT* preo) const
{
    return new CEmergenceCntrItem(preo, (CEmergenceDoc*) this);
}

void CEmergenceDoc::Serialize(CArchive& ar)
{
    CRichEditDoc::Serialize(ar);
}

#ifdef _DEBUG
void CEmergenceDoc::AssertValid() const
{
    CRichEditDoc::AssertValid();
}

void CEmergenceDoc::Dump(CDumpContext& dc) const
{
    CRichEditDoc::Dump(dc);
}
#endif //_DEBUG

cntritem.cpp

#include "stdafx.h"
#include "Emergence.h"

#include "EmergenceDoc.h"
#include "EmergenceView.h"
#include "cntritem.h"

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

IMPLEMENT_SERIAL(CEmergenceCntrItem, CRichEditCntrItem, 0)

CEmergenceCntrItem::CEmergenceCntrItem(REOBJECT *preo, CEmergenceDoc* pContainer)
    : CRichEditCntrItem(preo, pContainer)
{
}

#ifdef _DEBUG
void CEmergenceCntrItem::AssertValid() const
{
    CRichEditCntrItem::AssertValid();
}

void CEmergenceCntrItem::Dump(CDumpContext& dc) const
{
    CRichEditCntrItem::Dump(dc);
}
#endif

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

可能是您的InitInstance中缺少对AfxOleInit的调用。

这就是OLE内容失败的原因。