Search code examples
mfcassertlistcontrol

CListControl Insert Column Debug Assertion Failure


In my application, I create a modal dialog that contains a mfc List Control. When I don't initialize any columns or items in the List Control, the dialog shows without error. When I try to add a column to the List Control, I get the following Debug Assertion Failed message:

Debug Assertion Failed!

If it helps, the breakpoint is at

_AFXCMN_INLINE int CListCtrl::InsertColumn(int nCol, const LVCOLUMN* pColumn) { ASSERT(::IsWindow(m_hWnd)); return (int) ::SendMessage(m_hWnd, LVM_INSERTCOLUMN, nCol, (LPARAM)pColumn); }

I am trying to add the column headers with the following code in OnInitDialog():

BOOL EventL::OnInitDialog()
{
    m_ListEventLog.InsertColumn(0, _T("Description"), LVCFMT_LEFT, 250);  //Failure happens HERE
    //m_ListEventLog.InsertColumn(0, "Description", LVCFMT_LEFT, 200, 0); //I have also tried things such as this.
    return FALSE;
}

I add column headers to other CListControls in my application in this way, without problems. The modal dialog is called with the code:

void ListOption::OnBnClickedEventLog()
{
    EventL eventLog;
    eventLog.DoModal();
}

Solution

  • Maybe you forgot to call the default function:

    BOOL EventL::OnInitDialog()
    {
        BOOL res = CDialog::OnInitDialog();
        m_ListEventLog.InsertColumn(0, _T("Description"), LVCFMT_LEFT, 250);  //Failure happens HERE
        //m_ListEventLog.InsertColumn(0, "Description", LVCFMT_LEFT, 200, 0); //I have also tried things such as this.
        return res; // or return FALSE;
    }
    

    That's why ASSERT(::IsWindow(m_hWnd)) fails, because m_hWnd of ListView control is not ready. m_hWnd of dialog would not be ready either.