create CImagelist for myCComboBoxEx, Debug Assertion Fails, no idea what is wrong?

bingbing457 40 Reputation points
2023-08-22T11:33:01.4133333+00:00

i created a class "myCComboBoxEx" derived from CComboBoxEx, and i was trying to create a CImagelist objective for myCComboBoxEx, when it is running, got a run-time-error:

User's image

my code is as following:

...........

//bing, added, create/load the imagelist for the CcomboBoxEx object
	m_imagelist1.Create(30, 30, ILC_COLOR, 2, 2);
	CBitmap m_bitmap_temp;

	m_bitmap_temp.LoadBitmapW(IDB_BITMAP1);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL);
	m_bitmap_temp.Detach();
	m_bitmap_temp.LoadBitmapW(IDB_BITMAP2);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL); //BING, test
	m_bitmap_temp.Detach();
	m_bitmap_temp.LoadBitmapW(IDB_BITMAP3);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL);
	m_bitmap_temp.Detach();
	m_bitmap_temp.LoadBitmapW(IDB_BITMAP4);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL);
	m_bitmap_temp.Detach();
	m_bitmap_temp.LoadBitmapW(IDB_BITMAP5);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL);
	m_bitmap_temp.Detach();
	m_bitmap_temp.LoadBitmapW(IDB_BITMAP6);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL);
	m_bitmap_temp.Detach();
	m_bitmap_temp.LoadBitmapW(IDB_BITMAP7);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL);
	m_bitmap_temp.Detach();
	m_bitmap_temp.LoadBitmapW(IDB_BITMAP8);
	m_imagelist1.Add(&m_bitmap_temp, (CBitmap*)NULL);
	m_bitmap_temp.Detach();

	m_bitmap_temp.DeleteObject(); //bing, to save memory space

	m_comboBoxEx1.SetImageList(&m_imagelist1); //bing, memory leaks? comboboxex bugs? or limits on images????????


	COMBOBOXEXITEM comboboxItem;
	CString  str;


when it runs to here, "Debug Assertion Fails" pops up....l....

m_comboBoxEx1.SetImageList(&m_imagelist1);

i searched so many articles but none of them could answer my question, what should i do?

thanks.

Developer technologies | .NET | .NET Runtime
Developer technologies | C++
Developer technologies | Visual Studio | Other
{count} votes

Accepted answer
  1. David Lowndes 2,640 Reputation points MVP
    2023-08-22T11:53:37.47+00:00

    If you select the Retry option, you'll probably see that the assertion is this one:

    _AFXCMN_INLINE CImageList* CComboBoxEx::SetImageList(_In_ CImageList* pImageList)
    	{ ASSERT(::IsWindow(m_hWnd)); return CImageList::FromHandle((HIMAGELIST) ::SendMessage(m_hWnd, CBEM_SETIMAGELIST, 0, (LPARAM)pImageList->GetSafeHandle())); }
    

    Which means that at the time you call SetImageList, the combo box window hasn't been created.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. RLWA32 49,551 Reputation points
    2023-08-22T11:50:13.07+00:00

    The line in afxcmn2.inl that has the ASSERT is checking for a valid window handle.

    It appears that the code is calling CComboBoxEx::SetImageList before the actual window for the control has been created.

    It's not clear where the method is being called. For example, calling it from your class constructor will cause this assertion.

    1 person found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.