MFC: SetIcon() problem if called twice
Posted on 2011-05-27
I have an MFC application. In one of the dialogs, there is a button. I set an icon for this button in OnInitDialog() like this:
m_btnBold.SetIcon(static_cast<HICON>(::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_BOLD_PRESSED), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR)));
m_btnBold.SetIcon(static_cast<HICON>(::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDI_BOLD), IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR)));
But also this code is called in the method that handles certain event in the dialog.
This causes a crash with a break here:
_CRTIMP void _cdecl _CrtDbgBreak(
There is an error message in the debug output window:
Second Chance Assertion Failed: File f:\dd\vctools\vc7libs\ship\atlmfc\include\afxwin1.inl, Line 395
Book.exe has triggered a breakpoint
If I call SetIcon code only once, everything works fine. I guess I am missing some cleanup, but what and where? Could you please help me with that?
P.S. The crash happens not when SetIcon() is executed, but when I press OK and exit the dialog and the method from where the dialog's DoModal() is called. Also, it happens not the first time, but exactly the fifth opening of the dialog.