CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Threaded View

  1. #1
    Join Date
    Dec 2008
    Posts
    8

    MFC ListBox problem

    I am trying to create my own listbox class but failed. Here is the source code:

    // MyListBox.h
    Code:
    class CMyListBox : public CListBox
    {
    	DECLARE_DYNAMIC(CMyListBox)
    
    public:
    	CMyListBox(void);
    	virtual ~CMyListBox(void);	
    	int AddDetailedString(const LPCTSTR lpszItem, const CString csDetail);
                     ...
    private:
    	CObArray				m_coDetails;
    };
    // MyListBox.cpp
    Code:
    IMPLEMENT_DYNAMIC(CMyListBox, CDialog)
    
    CMyListBox::CMyListBox(void) {}
    
    CMyListBox::~CMyListBox(void) {}
    
    int CMyListBox::AddDetailedString(const LPCTSTR lpszItem, const CString csDetail)
    {
    	LPCTSTR lpszDetail = csDetail;
    	m_coDetails.Add((CObject*)lpszDetail); // this line always crash
    	return CListBox::AddString(lpszItem);
    }
    I have no idea why if I delcared a private array (or vector) variable and then push the elements to it. It always crash! If I use CObArray, I got "privileged instruction" promopt and debugger run at "AfxAssertValidObject". If I use std::vector, I got "Access Violation" and debugger run at "CheckBytes".

    Please could anyone give me some suggestion to solve this problem?

    Many thanks!
    Last edited by chesschi; December 7th, 2011 at 12:22 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured