Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My MFC program threw an exception. When I tried to debug the compiler pointed to a line in afxtempl.h.

Perhaps, I should give more details. The program is an MDI MFC program that creates three splitters. In the "CChildFrame::OnCreateClient" code shown below, the program successfully creates the first stated pane, but unable to create the second pane.Can any one give me some guidance.(The previous paragraph was added at a much later revision)

C++
template<class TYPE, class ARG_TYPE>
AFX_INLINE TYPE& CArray<TYPE, ARG_TYPE>::ElementAt(INT_PTR nIndex)
{ 
	ASSERT(nIndex >= 0 && nIndex < m_nSize);//This is the line it pointed to
	if(nIndex >= 0 && nIndex < m_nSize)
		return m_pData[nIndex]; 
	AfxThrowInvalidArgException();		
}


The above function is a memeber function of:

C++
template<class TYPE, class ARG_TYPE = const TYPE&>
class CArray 


Honestly, I don't know how to go about location these bug. The program keep running succesfully to cursor located past all the places I suspected the bug might be located. Meaning that all the location I feel the bug could be tested negetive for the bug.
Can anyone give a guidance ?


Further debugging produced the following discovery:

C++
BOOL CChildFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
	if( !CMDIChildWndEx::OnCreateClient(lpcs,pContext))
	{
		return FALSE;
	}
	
	if(!m_wndSplitter->CreateStatic(this,1,3,WS_CHILD))
	{
		return FALSE;
	}


	//Calculate the size of splitter
	SIZE size;
	CRect rect;
	GetClientRect(&rect);
	size.cx = (rect.right - m_wndSplitter->GetSplitterWidth())/3;
	size.cy = rect.bottom;

	//Set the views
	m_wndSplitter->CreateView(0,1,RUNTIME_CLASS(CClassManagerView),size,pContext);
	//Code successfully ran to this point
	m_wndSplitter->CreateView(0,2,RUNTIME_CLASS(CCalcView),size,pContext);
        //code failed to run to this point
	m_wndSplitter->CreateView(0,0,RUNTIME_CLASS(CDisplayView),size,pContext);

	SetActiveView((CView *)m_wndSplitter->GetPane(1,2));

	//Show the splitter
	m_wndSplitter->ShowWindow(SW_SHOWNORMAL);
	m_wndSplitter->UpdateWindow();

	return TRUE;
}


I tried to step into the line of code that failed to execute, but visual studio only pointed to "GetThisClass" and just simply sent me to the .cpp code of the class.
Can anyone help me with these further revealations.

By the way, futher debugging showed that the code:

C++
int CCalcView::OnCreate(LPCREATESTRUCT lpCreateStruct)

ran successfully.
Posted
Updated 24-May-14 3:36am
v4

1 solution

Start with the debugger: ignore the line it's reporting the problem on, and look through the call stack to find the line of your code that "caused" the problem. Chances are it's code near that, but it will almost certainly be a data problem of some sort - so look at the index value, and at the object it's indexing and try to work out why the assert fails. It'll be either that the index is negative or bigger than the collection, so all you need to do then is work out why!

We can't help you here: we can't run your code (we haven't got it) and we don't have access to your data (we can't see your screen, access your HDD, or read your mind).
 
Share this answer
 
Comments
Gbenbam 24-May-14 9:24am    
Do you think you can offer additional help with the just added new revelations?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900