1
votes

I'm not familiar with MFC but currently I must continue a project that was written in MFC. Now I'm having trouble with the following line when debugging

m_hIcon = AfxGetApp()->LoadIcon (IDR_MAINFRAME);

It always stops after the error "Assertion Failed: at afxwin1.inl". If I put a breakpoint there I saw a NULL icon handle. I tried running in release mode and it worked just fine although the handle is still NULL. I've read this question but my program is not a static library. It's a program that use a dll to connect to a CAN bus device. And the resource IDR_MAINFRAME is already in the project. It contains the default MFC icons. How can I solve this problem?


I've tried debugging and see that pModuleState changes between the first load program name call and the second load icon call. The first call returns successfully because pModuleState points to an object that has valid handle. But in the icon load call, pModuleState points to some object contains NULL handle. I also tried putting AFX_MANAGE_STATE(AfxGetStaticModuleState( )); right above the LoadIcon() call but the problem still arises


I've known the cause of this problem

UINT __cdecl RunCPRead(LPVOID pParam)
{
    CMyDlg *thisclass = (CMyDlg *)pParam;

    while (thisclass->m_Start)
    {   
        thisclass->GetData();
    }
    return 0;
}

AfxBeginThread(&RunCPRead, this, THREAD_PRIORITY_NORMAL, 0, 0, NULL);

After the GetData() call in RunCPRead, the control flows directly to CMyDlg's contructor although there's no object being created or copied

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
    : CDialog(CMyDlg::IDD, pParent)
{
    m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

and then fails at the assignment to m_hIcon with the error "access violation while reading". I've seen the disassembly, it was the line mov dword ptr [esi+90h], eax and it's inherently a write to memory.

I don't know why. How can I solve this?

3
Assertions are disabled in release mode, that's why the program appears to run fine in that mode. You probably should add an icon with identifier IDR_MAINFRAME to your resources file.Frédéric Hamidi
@FrédéricHamidi: In the resource list there is already IDR_MAINFRAME with many icons in itphuclv
That's strange. Are you sure these resources are actually embedded in your executable? (You can open the executable itself in resources view to double-check.)Frédéric Hamidi
@FrédéricHamidi: Yes, the resource is also embedded in the exe filephuclv

3 Answers

0
votes

The MFC code need the correct module handle to load the resource. Please try to read Afx*G/S*etResourceHandle.

0
votes

By default MFC uses the resource handle of the main application, not of the DLL. If you are making the call in the DLL then add this line at the start of the exported DLL function:

AFX_MANAGE_STATE(AfxGetStaticModuleState( ));

There is more information about this here:

http://msdn.microsoft.com/en-us/library/ba9d5yh5(v=vs.110).aspx

0
votes

Assertion Errors in MFC usually happens when wrong settings are set.

go to project settings > linker > System and change subsystem to (/SUBSYSTEM:WINDOWS) this solution solved my own problem.