Solved

CListCtrl::InsertItem() fail.

Posted on 1998-05-16
5
1,612 Views
Last Modified: 2013-11-20
I am trying to insert some strings into a list control(report type) and when I do the call to
InsertITem, I get the following error : "Access violation". I have two classes for two views
in a splitter window. Why does the function call crashes?, Am I doing something wrong?
Please HEEEEEELP!!!!.

// Class A
MyClass A : public CFormView
{
    public:
            void         FilltheList();
            CListCtrl m_listControl;
}

// In the Implementation file ....
void FilltheList()
{
      // tried two ways and they both failed
      m_listControl.InsertItem(&lvitem);
      m_listControl.InsertItem(offset, string);
}

//Class B
MyClassB : public CFormView
{
      protected:
          void GetProject();
}

//Implementation file
void GetProject()
{
     MyClassA* ptrclassA;

     ptrclassA->FilltheList();
}
0
Comment
Question by:jhromero
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 

Accepted Solution

by:
mweagle earned 20 total points
ID: 1311598
There are three different things which I think might cause this:

(1) You should make sure that you set the number of columns (InsertColumn(nCol, title)) in the list before trying to insert anything.  I usually put this in CFormView::OnInitialUpdate

(2) You can't insert anything until the list control window actually exists.  Therefore, you should check this condition before inserting items.  I usually put a FillTheList function in CFormView::OnUpdate and if m_cListCtrl.GetSafeHwnd() == NULL, I skip it.

(3) Here is some code I use to insert items.  I'm not sure if you snipped stuff out of your fill function, but this has worked for me.

      LV_ITEM item;
      item.mask = LVIF_TEXT;       // Only care about the text
      item.iSubItem = 0;           // Column 0
        item.pszText = "Insert Me";

      m_cListCtrl.InsertItem(&item);
0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1311599
When reporting an access violation, it's imperative to provdie a stack trace. You need to be initializing that structure before you send it to the control; a mistake there, or an incorrect initialization, is the most likely cause of your problem.

Why didn't you provide the code you used to initialize the structure?  You don't even show the declaration!  Why

Since the reported symptom was an access violation and not an assertion, mweagle's #2 is the wrong answer. MFC's CListCtrl::InsertItem() implementation ASSERTs() on the window handle being valid before trying to send the insertion message to the control. If the window didn't exist, the ASSERT would fire.

.B ekiM




0
 
LVL 11

Expert Comment

by:mikeblas
ID: 1311600
The code you've posted is obviously not the code you're really using because it is rife with syntax errors; the code you show isn't syntactically correct. In fact, it's completely usesless because we can't decide what's an intentional error and what's not.

For example, you coded this:

void GetProject()
{
   MyClassA* ptrclassA;
   ptrclassA->FilltheList();
}

but how do you initialize ptrclassA? You can't just call a member function through a pointe that's not pointing at anything.

.B ekiM


0
 

Expert Comment

by:mweagle
ID: 1311601
I agree with you Mike in that you can't tell what is causing the problem since the posted code is invalid ("snipped stuff").  However, since his ultimate goal was to get items into the list, I tried to provide some ideas on how to accomplish this, even if they didn't directly address the violation.  Someday I'll get the hang of this...unfortunately that day is not today :(
0
 

Author Comment

by:jhromero
ID: 1311602
Hi, sorry about not providing more info., but I just wanted to keep it short.  At any rate, your second suggetion makes sense to me.  Below is the actual code:

The child window is a splitter. One side contains the list control and the other side
contains a set of Edit controls (CinputView).

// Implemetation of CInput:public CFormView
void CInputView::GetFiles()
{
    CStdioFile   fileStr;   // store the name of the fiel
    CString      pString; // store each line
    int entries;

    TRY
    {
        fileStr.Open(m_projectPath, CFile::modeRead);
        TRACE1("The name of the file is %s\n",
                    fileStr.GetFileName());
    }
    CATCH (CFileException, e)
    {
        if (e->m_cause == CFileException::fileNotFound)
        {
            TRACE0("Unknown exception reading file!\n");
            throw;
        }
        e->Delete();
    }
    END_CATCH
   
    // Get a pointer to the list control
    CFileListCtrl*  ptrList;

    entries = 0;
    while (fileStr.ReadString(pString)!= NULL)
    {
        if ( pString.Find("[State]") != -1)
        {
            TRACE1("The string found is : %s\n", pString);
            break;
        }
        //Insert items into the list control box
        entries++;
        ptrList->FillListCtrl(entries);
    }

}

//===============================================
//                 Header file
//===============================================
class CFileListCtrl : public CFormView
{
protected:
      CFileListCtrl();           // protected constructor used by dynamic creation
      DECLARE_DYNCREATE(CFileListCtrl)

// Form Data
public:
      //{{AFX_DATA(CFileListCtrl)
      enum { IDD = IDD_LIST_DLG };
      CListCtrl         m_listControl;
      //}}AFX_DATA

// Attributes
public:

// Operations
public:
      void FillListCtrl(int);

// Overrides
      // ClassWizard generated virtual function overrides
      //{{AFX_VIRTUAL(CFileListCtrl)
      public:
      virtual void OnInitialUpdate();
      protected:
      virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
      //}}AFX_VIRTUAL

// Implementation
protected:
      virtual ~CFileListCtrl();
#ifdef _DEBUG
      virtual void AssertValid() const;
      virtual void Dump(CDumpContext& dc) const;
#endif

      // Generated message map functions
      //{{AFX_MSG(CFileListCtrl)
            // NOTE - the ClassWizard will add and remove member functions here.
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};

//------------ Implementation of the CFileListCtrl -------------------------------
void CFileListCtrl::OnInitialUpdate()
{
      CFormView::OnInitialUpdate();

    TCHAR   rgtsz[4][10]={_T("Name"), _T("Owner"),
                          _T("Modified"), _T("Size")};

    CRect       rect;
    LV_COLUMN   lvcolumn;
    int i;

    m_listControl.GetWindowRect(&rect);

    for (i=0; i<4; i++)
    {
        lvcolumn.mask = LVCF_FMT|LVCF_SUBITEM|
                        LVCF_TEXT|LVCF_WIDTH;
        lvcolumn.fmt =LVCFMT_LEFT;
        lvcolumn.pszText= rgtsz[i];
        lvcolumn.iSubItem = i;
        lvcolumn.cx = rect.Width()/4;
        m_listControl.InsertColumn(i, &lvcolumn);
    }
      
}

void CFileListCtrl::FillListCtrl(int numOfItems)
{
    LPTSTR  lstItem = "Hello";
    LV_ITEM lvitem;
    int item;

    for (item=0; item < numOfItems; item++)
    {
        lvitem.mask = LVIF_TEXT;
        lvitem.iItem = item;
        lvitem.pszText = lstItem;
        m_listControl.InsertItem(&lvitem);
    }

}

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Define unique primary key 9 95
Detect CR LF to each line 12 176
difference between String.subString() and String.subSequence() 6 273
Thin secure Windows 10 5 108
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question