CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2012
    Posts
    3

    debug assertion failed during mfc regular dll startup

    I'm working on a project that's using an mfc regular dll from a Qt gui project(yes, you don't see wrong), the mfc dll code is extracted from some other projects build entirely in mfc. I build all the mfc code with a regular dll and run it, and it came to the debug assertion during the initialization at the code:

    ASSERT(AfxGetModuleState() != AfxGetAppModuleState());

    and

    VERIFY(AfxSetModuleState(AfxGetThreadState()->m_pPrevModuleState) == &afxModuleState);

    furthermore, when i ignore and continue running the programme, a debug assertion failed at the code:

    ASSERT(afxCurrentResourceHandle != NULL,

    of course the resource handle is null.

    I track down the source code, and it seems that during the initialization, the 'dllmain'(in fact there is a lot of 'dllmains') in my dll has been invoked twice and only the seconde time the assert has been emitted, the first time it has been running ok. and I notice that the members in the global variable afxModuleState has all been null thus cause the assertion after the initialization, and what's more I notice that during the first time initialization, the afxModuleState has been assigned right.

    I wonder what's the cause of my problem and how to solve it. And I'm willing to hear your advice.

    an additional information is that, in my regular dll, I use boost library(which I'm not too familiar with)

  2. #2
    Join Date
    Jan 2012
    Posts
    3

    Re: debug assertion failed during mfc regular dll startup

    ps.

    the visual studio version i use is 2005

  3. #3
    Join Date
    Jan 2012
    Posts
    3

    Re: debug assertion failed during mfc regular dll startup

    the fisrst time initialization call stack:

    mfc80d.dll!AfxWinInit(HINSTANCE__ * hInstance=0x781d0000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x781d2aac, int nCmdShow=0) 行30 C++
    mfc80d.dll!DllMain(HINSTANCE__ * hInstance=0x781d0000, unsigned long dwReason=1, void * __formal=0x0012fd24) 行504 C++
    mfc80d.dll!__DllMainCRTStartup(void * hDllHandle=0x781d0000, unsigned long dwReason=1, void * lpreserved=0x0012fd24) 行495 + 0x11 字节 C
    mfc80d.dll!_DllMainCRTStartup(void * hDllHandle=0x781d0000, unsigned long dwReason=1, void * lpreserved=0x0012fd24) 行459 + 0x11 字节 C

    the second time initialization call stack:

    GemsDLL.dll!InternalDllMain(HINSTANCE__ * hInstance=0x00710000, unsigned long dwReason=1, void * __formal=0x0012fd24) 行100 C++
    GemsDLL.dll!DllMain(HINSTANCE__ * hInstance=0x00710000, unsigned long dwReason=1, void * lpReserved=0x0012fd24) 行283 C++
    GemsDLL.dll!__DllMainCRTStartup(void * hDllHandle=0x00710000, unsigned long dwReason=1, void * lpreserved=0x0012fd24) 行495 + 0x11 字节 C
    GemsDLL.dll!_DllMainCRTStartup(void * hDllHandle=0x00710000, unsigned long dwReason=1, void * lpreserved=0x0012fd24) 行459 + 0x11 字节 C

    the call stack when the assert 'ASSERT(AfxGetModuleState() != AfxGetAppModuleState())' occurs:

    mfc80d.dll!AfxGetModuleState() 行422 C++
    mfc80d.dll!AfxCoreInitModule() 行575 + 0x5 字节 C++
    GemsDLL.dll!InternalDllMain(HINSTANCE__ * hInstance=0x00710000, unsigned long dwReason=1, void * __formal=0x0012fd24) 行107 C++
    GemsDLL.dll!DllMain(HINSTANCE__ * hInstance=0x00710000, unsigned long dwReason=1, void * lpReserved=0x0012fd24) 行283 C++
    GemsDLL.dll!__DllMainCRTStartup(void * hDllHandle=0x00710000, unsigned long dwReason=1, void * lpreserved=0x0012fd24) 行495 + 0x11 字节 C
    GemsDLL.dll!_DllMainCRTStartup(void * hDllHandle=0x00710000, unsigned long dwReason=1, void * lpreserved=0x0012fd24) 行459 + 0x11 字节 C

    in code

    AFX_MODULE_STATE* AFXAPI AfxGetModuleState()
    {
    _AFX_THREAD_STATE* pState = _afxThreadState;
    ENSURE(pState);
    AFX_MODULE_STATE* pResult;
    if (pState->m_pModuleState != NULL)
    {
    // thread state's module state serves as override
    pResult = pState->m_pModuleState;
    }
    else
    {
    // otherwise, use global app state
    pResult = _afxBaseModuleState.GetData();
    }
    ENSURE(pResult != NULL);
    return pResult;
    }

    the pState is different from the first time initialization(the first time is not my mfc dll?), and all its value is null

    - pState 0x001b44c8 {m_pModuleState=0x00000000 m_pPrevModuleState=0x00000000 m_pSafetyPoolBuffer=0x00000000 ...} _AFX_THREAD_STATE *
    + CNoTrackObject {...} CNoTrackObject
    - m_pModuleState 0x00000000 {m_pCurrentWinApp=??? m_hCurrentInstanceHandle=??? m_hCurrentResourceHandle=??? ...} AFX_MODULE_STATE *
    + CNoTrackObject {...} CNoTrackObject
    m_pCurrentWinApp CXX0030: 错误: 无法计算表达式的值
    m_hCurrentInstanceHandle CXX0030: 错误: 无法计算表达式的值
    m_hCurrentResourceHandle CXX0030: 错误: 无法计算表达式的值
    m_lpszCurrentAppName CXX0030: 错误: 无法计算表达式的值
    m_bDLL CXX0030: 错误: 无法计算表达式的值
    m_bSystem CXX0030: 错误: 无法计算表达式的值
    + m_bReserved 0x00000016 <错误的指针> unsigned char [2]
    m_fRegisteredClasses CXX0030: 错误: 无法计算表达式的值

  4. #4
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: debug assertion failed during mfc regular dll startup

    You might have to take a look at AFX_MANAGE_STATE.
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  5. #5
    Join Date
    Oct 2012
    Posts
    1

    Re: debug assertion failed during mfc regular dll startup

    Quote Originally Posted by Marc G View Post
    You might have to take a look at AFX_MANAGE_STATE.
    Sorry for bringing up an old topic but I have this problem as well and I don't understand it, even after reading the site referred to.

    My situation is I have a DLL that works fine initially. Then I begin using a 3rd party lib file statically linked into my dll. As long as I don't reference anything in the library my code still compiles, links and runs without any problem. But as soon as I include a single line using the library I get the crash on startup as well. My callstack is this:

    > mfc90d.dll!AfxCoreInitModule() Line 587 + 0x23 bytes C++
    ControlClient.dll!InternalDllMain(HINSTANCE__ * hInstance=0x06e20000, unsigned long dwReason=1, void * __formal=0x00000000) Line 88 C++
    ControlClient.dll!DllMain(HINSTANCE__ * hInstance=0x06e20000, unsigned long dwReason=1, void * lpReserved=0x00000000) Line 272 C++
    ControlClient.dll!__DllMainCRTStartup(void * hDllHandle=0x06e20000, unsigned long dwReason=1, void * lpreserved=0x00000000) Line 546 + 0x11 bytes C
    ControlClient.dll!_DllMainCRTStartup(void * hDllHandle=0x06e20000, unsigned long dwReason=1, void * lpreserved=0x00000000) Line 510 + 0x11 bytes C
    ntdll.dll!77a49950()

    (and so on into other system libraries)

    So the crash is on startup before I ever get a chance to use the AFX_MANAGE_STATE(AfxGetStaticModuleState()) macro.

    Can someone explain this in more detail? Because I don't understand it from the MSDN explanation (which as far as I can see doesn't offer any explanation on exactly when you need to do this).

    Thanks in advance.

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