Click here to Skip to main content
15,075,944 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Project: MFC-SDI-C ++ (VS2012)

Hi everybody Im new programming in MFC i have the next:

1. I have built a CListCtrl-looking as a report on a CDialog (The CListCtrl was added using the controls toolbox). The CDialogEx is on a CDockablePanel that are created when the program is opened and always is showed. I added columns and items to CListCtrl to be created without any problems (Class: CCoordsListDlg).

2. I have created a different class that generate double type data (Class: CCoordsGenerator). These data are generated catching the mouse click screen coordinates without any problems. These data should be added on the the CListCtrl as subitems.

3. To add the generate data on the CListCtrl, i have a method that is called from the CCoordsGenerator class but when the method that add the subitems run a DEBUG ASSERTION appears.

4. The DEBUG ASSERTION stop in: ASSERT(::IsWindow(m_hWnd)); of the winctrl2.cpp file in the next section:

BOOL CListCtrl::SetItemText(int nItem, int nSubItem, LPCTSTR lpszText)
	ASSERT((GetStyle() & LVS_OWNERDATA)==0);
	LVITEM lvi;
	lvi.iSubItem = nSubItem;
	lvi.pszText = (LPTSTR) lpszText;
	return (BOOL) ::SendMessage(m_hWnd, LVM_SETITEMTEXT, nItem, (LPARAM)&lvi);

The class code where the CListCtrl is created is the next (CCoordsListDlg.cpp):
BOOL CCoordsListDlg::OnInitDialog()

	// TODO:  Add extra initialization here

	CListCtrl m_coordslist;

	// The tree columns are created
	// The column where the points will be displayed is created
	// The column where the point x-coord will be displayed is created
	m_coordslist.InsertColumn(1,"x coord");
	// The column where the point y-coord will be displayed is created
	m_coordslist.InsertColumn(2,"y coord");
    // The points are added
	return TRUE; 
	// EXCEPTION: OCX Property Pages should return FALSE

A portion of the code where the data are generated is (CCoordsGenerator.cpp):

void CCoordsGenerator::OnLeftButtonDown()
    // Get the pickposition
	double q[2];

	// Share pick position
	x_coord = q[0];
	y_coord = q[1];
	// The coordinates value are added as subitems to a CListCtrl
	CCoordsListDlg m_coordDlg;
	m_coordDlg.RWImageInteractor(row, x_coord, y_coord); //<<<< HERE IS WHEN THE COORDS ARE SENDED TO A CListCtrl >>>>
	// Draw a sphere on the point
	createSpherePoint(row, x_coord,y_coord);

The method of the class CCoordsListDlg that add the subitems is:

void CCoordsListDlg::RWImageInteractor(int row, double x_coord, double y_coord)
    // Converting the double variable to LPTSTR
    char x_buff[8],

    // The coordinates are added to list as subitems

Could you help me to understand where my error is?
Thank you all so much for your help

What I have tried:

really I don´t know what is the trouble with my code. I've tried to use the LVITEM methosd to add the subitems and put progamatically the focus on the CListCtrl but the assertion always appears.
Updated 5-Jun-16 21:38pm
George Jonsson 5-Jun-16 18:44pm
What is the value of m_hWnd?
siqueirosjorge 5-Jun-16 19:32pm
Hi George, i have added a Watch on the m_hWnd, the result when the Assertion appear is 0x000000{unused=???}-unable to read memory - Thanks for your comment.
barneyman 5-Jun-16 19:54pm
i don't see anywhere where you're creating, or attaching to, that list control (which is LOCAL to the OnInit... it's obscuring the global)
siqueirosjorge 5-Jun-16 20:30pm
Hi Barneyman thanks for your comment, i´ve attempted the Georges Jonsson comment. I have changed the code where the CListCtl extended style is defined by the function CreateEx that is where the CListCtl is created programatically but the assertion continues appearsing
barneyman 5-Jun-16 20:55pm
because you're still messing with the local decl (of m_coordslist inside OnInit..) and not the member
Philippe Mori 6-Jun-16 18:08pm
Item text is probably called too early before the control is created.

The list control instance must be a member of your dialog class. Actually you are creating a new local instance in OnInitDialog using that instead of the class wide member.

A typical implementation would look like:
// CCoordsListDlg.h

class CCoordsListDlg : public CDialogEx
// ...
    CListCtrl m_coordslist;
// ...

// CCoordsListDlg.cpp

void CCoordsListDlg::DoDataExchange(CDataExchange* pDX)
    // When list is defined by dialog resource template.
    // If not, it must be created / sized in OnInitDialog.
    DDX_Control(pDX, IDC_OF_LIST, m_coordslist);
    // ...

BOOL CCoordsListDlg::OnInitDialog()
    // This would create a new local instance. Remove it.
    //CListCtrl m_coordslist;
// ...

Similar may apply here where a new local CCoordsListDlg is created but not shown:
void CCoordsGenerator::OnLeftButtonDown()
    // ...
    // This looks suspicious too.
    // Remove it if m_coordDlg is a class member and dialog already shown
    //  or show dialog here.
    CCoordsListDlg m_coordDlg;
    m_coordDlg.RWImageInteractor(row, x_coord, y_coord); //<<<< HERE IS WHEN THE COORDS ARE SENDED TO A CListCtrl >>>>
    // ...
I cannot be entirely sure, because I haven't made any MFC projects in 15 years or so, but I think you need to call the Create method in order to set the parent window.

See CListCtrl::Create[^]
or maybe CListCtrl::CreateEx[^]

This code
CListCtrl m_coordslist;

should be changed to something like
CListCtrl m_coordslist;
    CRect(10,10,400,200), pParentWnd, IDD_SOME_ID)); 

Where pParentWnd is an instance of CCoordsListDlg and IDD_SOME_ID is the ID of your list control.
siqueirosjorge 5-Jun-16 20:23pm
Thanks a lot for your comment George, I've changed the code that you suggest. However the assertion continues appearsing.
George Jonsson 5-Jun-16 20:39pm
Then you have to dig into the code with the debugger and try to find out why m_hWnd is NULL.
siqueirosjorge 5-Jun-16 20:52pm
ok. i'll start to dig into the code. Thanks for your help George. Regards

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