Debug Assertion Error

Hi y'all, i'm back with another question. :)
As I described previously(http://www.cplusplus.com/forum/beginner/120171/), I' m creating a simple MFC text editor in VS2010.

Though everything compiles, when I run the program in debug mode, it gives me a "Debug Assertion Failed" Error on line 151, 156 and 1875 of viewrich.cpp which is part of the MFC for VS2010.

Line 151 and 156 are among the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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;
}


Line 1875 is among the following code:

1
2
3
4
5
6
7
8
9
10
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());
}


Below are the relevant files again for your viewing pleasure. :)

cntritem.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#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 


EmergenceDoc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#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"

///////////////////////////////////////////////////////////////////////////
// CMyWordDoc

IMPLEMENT_DYNCREATE(CEmergenceDoc, CRichEditDoc)

BEGIN_MESSAGE_MAP(CEmergenceDoc, CRichEditDoc)
    //{{AFX_MSG_MAP(CMyWordDoc)
        // NOTE - the ClassWizard will add and remove mapping macros here.
        //    DO NOT EDIT what you see in these blocks of generated code!
    //}}AFX_MSG_MAP
    // Enable default OLE container implementation
    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()

///////////////////////////////////////////////////////////////////////////
// CMyWordDoc construction/destruction

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);
}

///////////////////////////////////////////////////////////////////////////
// CMyWordDoc serialization

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

///////////////////////////////////////////////////////////////////////////
// CMyWordDoc diagnostics

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

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


cntritem.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#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

/////////////////////////////////////////////////////////////////////////////
// CWordPadCntrItem implementation

IMPLEMENT_SERIAL(CEmergenceCntrItem, CRichEditCntrItem, 0)

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

/////////////////////////////////////////////////////////////////////////////
// CWordPadCntrItem diagnostics

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

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


Thanks for tuning in..... :)
From the fact that it gives me a "Debug Assertion Failed" Error on line 151, 156 and 1875 of viewrich.cpp, i'm guessing the error has something to do with rich edit ole. But I'm a beginner and its a bit overwhelming trying to figure out what I'm missing and how it all fits together..... :(
this looks fishy:
1
2
3
4
	CEmergenceDoc* GetDocument()
		{ return (CEmergenceDoc*)COleClientItem::GetDocument(); }
	CEmergenceView* GetActiveView()
		{ return (CEmergenceView*)COleClientItem::GetActiveView(); }
Does COleClientItem::GetDocument() really return CEmergenceDoc? Better use dynamic_cast<>() instead.


This is something you shouldn't do either:
1
2
3
4
CRichEditCntrItem* CEmergenceDoc::CreateClientItem(REOBJECT* preo) const
{
    return new CEmergenceCntrItem(preo, (CEmergenceDoc*) this); // cast away const is usually bad
}
Thanks for replying.

I did the dynamic cast on COleClientItem::GetDocument() but it still gave me the same error...It wouldn't allow me to do the dynamic cast on COleClientItem::GetActiveView(); saying that I needed to define the class CEmergenceView(which I have already defined - as can be seen above - so I don't know what's that about).

In regards to the cast away const, how do I modify

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


so that it is still compliant?
It wouldn't allow me to do the dynamic cast on COleClientItem::GetActiveView();
Well, never cast away an compiler error. It's not that much of a surprise that you get the "Debug Assertion Failed" exactly where you get the compiler error.

So use dynamic_cast and find the reason for the error. What exactly is the error?


In regards to the cast away const, how do I modify
That depends on why you want that pointer
Topic archived. No new replies allowed.