Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

I am getting assertion error, on implementing the updates for "Close All Others" in MFC Application,, wherein, Tab Document Interface(TDI) has numerous tabs, similar to VS 2008 IDE, and when user right clicks the context menu <<Close All Other>> , which has the following handler ,

The following code will close all except the active tab in the "first" MDI client tab group..
//Mainframe.cpp

Begin_message_map

..............

ON_COMMAND(ID_TAB_CLOSEOTHER, &CMainFrame::OnTabCloseOther)

............

CMainFrame::OnCloseOtherTab()

{

Line 1. const CObList* tabList = &m_wndClientArea.GetMDITabGroups(); 
Line 2: CMFCTabCtrl* pTabWnd = (CMFCTabCtrl*)tabList->GetHead(); 
Line 3. if (pTabWnd != NULL) 
Line 4: { 
Line 5: int iActiveTab = pTabWnd->GetActiveTab();
Line 6: ASSERT_VALID(pTabWnd);
Line 7: for (int i = pTabWnd->GetTabsNum() - 1; i >= 0; i--)
Line 8: {
Line 9: CMDIChildWndEx* pNextWnd = DYNAMIC_DOWNCAST CMDIChildWndEx........................ }
//MainFrame.h 

CMDIChientAreaWnd m_wndClientArea; 



The assertion is displayed on execution of Line 2. The following line gets displayed on assertion
return AfxWinMain(hInstance, hPrevInstance, lpCmdLine,nCmdShow);

<<<<Unhandled exception at 0x787d01ec (mfc90d.dll)
in CART.exe: 0xC0000005: Access violation reading location x00000008.>>>>

Please let me know how to correct the above solution.

With Regards,
Posted
Updated 11-Jul-11 5:18am
v2

The documentation for CObList::GetHead() says that the list cannot be empty and would fail.

You are not currently checking the size of the list before getting the head element. Try calling IsEmpty() first to determine whether accessing the head element would be an error.

It may be worth stepping through the code to check the the tabList pointer is valid from the call to GetMDITabGroups
 
Share this answer
 
AfxWinMain() is the entry point of an MFC application, are you sure you're not accidentally calling this method during startup? You may have a conflict with ID_TAB_CLOSEOTHER or something is calling that directly upon startup but before the members are initialized.
 
Share this answer
 

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