Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Debug assertion when using AFX_MANAGE_STATE(AfxGetStaticModuleState())

Debug assertion when using AFX_MANAGE_STATE(AfxGetStaticModuleState())

Scheduled Pinned Locked Moved C / C++ / MFC
helpdebugginglearning
10 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    panzerdivisionmarkus
    wrote on last edited by
    #1

    Hi, I have a wierd problem that I hope someone here can help me out with. And if I posted in the wrong forum please redirect me. Anyway, I have a program where I load a Dll, which then loads a bunch of other dlls. I recently did some changes so that the program would support unicode. After the changes I got these wierd problems: One of the dlls causes a debug assertion failed when loading from it's resource, and another doesn't. They both use the following code:

    HRESULT CPictureWnd::LoadResource(WORD wId, LANGID wLangId) {
    LPCTSTR sTemplateName = MAKEINTRESOURCE(wId);
    HINSTANCE hInst = AfxFindResourceHandle(sTemplateName, _T("GIF_IMAGE"));

    HRSRC hResInfo = ::FindResourceEx(hInst, \_T("GIF\_IMAGE"), sTemplateName, wLangId); 
    if (!hResInfo) hResInfo = ::FindResourceEx(hInst, \_T("GIF\_IMAGE"), sTemplateName, 0x409); 
    

    ...(other code)
    return hr;
    }

    The Dll that manages to call the function uses this code:

    CTM_MODULE_API BOOL module_mtype_init(LPMODULE_MTYPE_DATA mtype)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    mtype->pUserData = (_MODULE_MTYPE_USER_DATA*)calloc(sizeof(_MODULE_MTYPE_USER_DATA),1);
    mtype->pUserData->m_picIcon = new CPictureWnd;
    mtype->pUserData->m_picIcon->LoadResource(IDB_PATHICON,mtype->langId);
    return TRUE;
    }

    And the one who gets the assertion uses this:

    CTM_MODULE_API BOOL module_mtype_init(LPMODULE_MTYPE_DATA mtype)
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    mtype->pUserData = (_MODULE_MTYPE_USER_DATA*)calloc(sizeof(_MODULE_MTYPE_USER_DATA),1);
    mtype->pUserData->m_picIcon = new CPictureWnd;
    mtype->pUserData->m_picIcon->LoadResource(IDB_PATHICON,mtype->langId);
    return TRUE;
    }

    I get the debug assertion failes at the following row: HINSTANCE hInst = AfxFindResourceHandle(sTemplateName, _T("GIF_IMAGE")); I also get an assertion failure in my dll which loads the other dlls in the following code:

    Cctm::Cctm(void)
    :m_lock("Cctm::lock")
    {
    AFX_MANAGE_STATE(AfxGetStaticModuleState());

    m_initialized=FALSE;
    ...other code

    //path to the DLL

    CString sFile;
    GetModuleFileName(AfxGetResourceHandle(), sFile.GetBuffer(_MAX_PATH + 1), _MAX_PATH);

    ...other code
    }

    The last row causes an assertion. If I comment out AFX_MANAGE_STATE(AfxGetStaticModuleState()); things seems to work better, but then it fetches texts from the wrong resource file

    G 1 Reply Last reply
    0
    • P panzerdivisionmarkus

      Hi, I have a wierd problem that I hope someone here can help me out with. And if I posted in the wrong forum please redirect me. Anyway, I have a program where I load a Dll, which then loads a bunch of other dlls. I recently did some changes so that the program would support unicode. After the changes I got these wierd problems: One of the dlls causes a debug assertion failed when loading from it's resource, and another doesn't. They both use the following code:

      HRESULT CPictureWnd::LoadResource(WORD wId, LANGID wLangId) {
      LPCTSTR sTemplateName = MAKEINTRESOURCE(wId);
      HINSTANCE hInst = AfxFindResourceHandle(sTemplateName, _T("GIF_IMAGE"));

      HRSRC hResInfo = ::FindResourceEx(hInst, \_T("GIF\_IMAGE"), sTemplateName, wLangId); 
      if (!hResInfo) hResInfo = ::FindResourceEx(hInst, \_T("GIF\_IMAGE"), sTemplateName, 0x409); 
      

      ...(other code)
      return hr;
      }

      The Dll that manages to call the function uses this code:

      CTM_MODULE_API BOOL module_mtype_init(LPMODULE_MTYPE_DATA mtype)
      {
      AFX_MANAGE_STATE(AfxGetStaticModuleState());
      mtype->pUserData = (_MODULE_MTYPE_USER_DATA*)calloc(sizeof(_MODULE_MTYPE_USER_DATA),1);
      mtype->pUserData->m_picIcon = new CPictureWnd;
      mtype->pUserData->m_picIcon->LoadResource(IDB_PATHICON,mtype->langId);
      return TRUE;
      }

      And the one who gets the assertion uses this:

      CTM_MODULE_API BOOL module_mtype_init(LPMODULE_MTYPE_DATA mtype)
      {
      AFX_MANAGE_STATE(AfxGetStaticModuleState());
      mtype->pUserData = (_MODULE_MTYPE_USER_DATA*)calloc(sizeof(_MODULE_MTYPE_USER_DATA),1);
      mtype->pUserData->m_picIcon = new CPictureWnd;
      mtype->pUserData->m_picIcon->LoadResource(IDB_PATHICON,mtype->langId);
      return TRUE;
      }

      I get the debug assertion failes at the following row: HINSTANCE hInst = AfxFindResourceHandle(sTemplateName, _T("GIF_IMAGE")); I also get an assertion failure in my dll which loads the other dlls in the following code:

      Cctm::Cctm(void)
      :m_lock("Cctm::lock")
      {
      AFX_MANAGE_STATE(AfxGetStaticModuleState());

      m_initialized=FALSE;
      ...other code

      //path to the DLL

      CString sFile;
      GetModuleFileName(AfxGetResourceHandle(), sFile.GetBuffer(_MAX_PATH + 1), _MAX_PATH);

      ...other code
      }

      The last row causes an assertion. If I comment out AFX_MANAGE_STATE(AfxGetStaticModuleState()); things seems to work better, but then it fetches texts from the wrong resource file

      G Offline
      G Offline
      Gary R Wheeler
      wrote on last edited by
      #2

      Is it possible one or more pieces might have been missed in the UNICODE conversion? Another issue might be string sizes, possibly in your _MODULE_MTYPE_USER_DATA structure. If one of the DLL's has that structure with _TCHAR arrays for strings, and the other has char arrays, that would make the structures different sizes, causing all sorts of Bad Things to happen due to buffer overruns. Did you redeclare your strings as arrays of _TCHAR's, or do you use CString's everywhere?

      Software Zen: delete this;
      Fold With Us![^]

      P 1 Reply Last reply
      0
      • G Gary R Wheeler

        Is it possible one or more pieces might have been missed in the UNICODE conversion? Another issue might be string sizes, possibly in your _MODULE_MTYPE_USER_DATA structure. If one of the DLL's has that structure with _TCHAR arrays for strings, and the other has char arrays, that would make the structures different sizes, causing all sorts of Bad Things to happen due to buffer overruns. Did you redeclare your strings as arrays of _TCHAR's, or do you use CString's everywhere?

        Software Zen: delete this;
        Fold With Us![^]

        P Offline
        P Offline
        panzerdivisionmarkus
        wrote on last edited by
        #3

        It's possible that I missed to redeclare some of the chars to TCHARs and messed up the sizing. Thanks for the tip, I'll have to check on that. But I don't understand why the GetModuleFileName casues an assetion when AFX_MANAGE_STATE(AfxGetStaticModuleState()) is used. As far as I can see that error shouldn't depend on any TCHAR/char mismatch.

        Cctm::Cctm(void)
        :m_lock("Cctm::lock")
        {
        AFX_MANAGE_STATE(AfxGetStaticModuleState());

        m_initialized=FALSE;
        m_session=NULL;
        m_sModuleStatus[0]=0;
        m_iApprovedModuleStatus = 0;
        m_sActivityStatus[0]=0;
        m_iApprovedActivityStatus = 0;
        m_showCode=TRUE;
        m_moduleTypes=NULL;
        m_usedDisplayMasks=0;
        m_dbinfo=NULL;

        std_functions_init();

        //path to the DLL
        HINSTANCE l = afxCurrentResourceHandle;
        CString sFile;
        GetModuleFileName(AfxGetResourceHandle(), sFile.GetBuffer(_MAX_PATH + 1), _MAX_PATH);

        Or could it have something to do with the sFile.GetBuffer(_MAX_PATH + 1)? Could that cause some wierd behaviout now when it's unicode?

        G 1 Reply Last reply
        0
        • P panzerdivisionmarkus

          It's possible that I missed to redeclare some of the chars to TCHARs and messed up the sizing. Thanks for the tip, I'll have to check on that. But I don't understand why the GetModuleFileName casues an assetion when AFX_MANAGE_STATE(AfxGetStaticModuleState()) is used. As far as I can see that error shouldn't depend on any TCHAR/char mismatch.

          Cctm::Cctm(void)
          :m_lock("Cctm::lock")
          {
          AFX_MANAGE_STATE(AfxGetStaticModuleState());

          m_initialized=FALSE;
          m_session=NULL;
          m_sModuleStatus[0]=0;
          m_iApprovedModuleStatus = 0;
          m_sActivityStatus[0]=0;
          m_iApprovedActivityStatus = 0;
          m_showCode=TRUE;
          m_moduleTypes=NULL;
          m_usedDisplayMasks=0;
          m_dbinfo=NULL;

          std_functions_init();

          //path to the DLL
          HINSTANCE l = afxCurrentResourceHandle;
          CString sFile;
          GetModuleFileName(AfxGetResourceHandle(), sFile.GetBuffer(_MAX_PATH + 1), _MAX_PATH);

          Or could it have something to do with the sFile.GetBuffer(_MAX_PATH + 1)? Could that cause some wierd behaviout now when it's unicode?

          G Offline
          G Offline
          Gary R Wheeler
          wrote on last edited by
          #4

          The other possibility is the AfxGetResourceHandle() call. Have you tried stepping into the method with the debugger?

          Software Zen: delete this;
          Fold With Us![^]

          P 1 Reply Last reply
          0
          • G Gary R Wheeler

            The other possibility is the AfxGetResourceHandle() call. Have you tried stepping into the method with the debugger?

            Software Zen: delete this;
            Fold With Us![^]

            P Offline
            P Offline
            panzerdivisionmarkus
            wrote on last edited by
            #5

            I tried to do that, I have a vague memory that I've managed to that before, but now the debugger couldn't find any source code, so I had to go to disassembly. Anyway, I managed to figure out that it uses afxCurrentResourceHandle, and when I use it in my code it return null, and thats probably why AfxGetResourceHandle() causes assrt failure. Isn't AFX_MANAGE_STATE(AfxGetStaticModuleState()) supposed to set me to the right resource? Do you know how can I get the handle to be a valid handle to my dll?

            G 1 Reply Last reply
            0
            • P panzerdivisionmarkus

              I tried to do that, I have a vague memory that I've managed to that before, but now the debugger couldn't find any source code, so I had to go to disassembly. Anyway, I managed to figure out that it uses afxCurrentResourceHandle, and when I use it in my code it return null, and thats probably why AfxGetResourceHandle() causes assrt failure. Isn't AFX_MANAGE_STATE(AfxGetStaticModuleState()) supposed to set me to the right resource? Do you know how can I get the handle to be a valid handle to my dll?

              G Offline
              G Offline
              Gary R Wheeler
              wrote on last edited by
              #6

              It sounds like one or more of your DLL's are release compiles. In order for you to debug into them, they all need to be debug builds.

              Software Zen: delete this;
              Fold With Us![^]

              P 2 Replies Last reply
              0
              • G Gary R Wheeler

                It sounds like one or more of your DLL's are release compiles. In order for you to debug into them, they all need to be debug builds.

                Software Zen: delete this;
                Fold With Us![^]

                P Offline
                P Offline
                panzerdivisionmarkus
                wrote on last edited by
                #7

                hmm...no, my Dlls are debug builds, but in that case it seems like the debugger can't find the MFC debug dlls where GetModuleFileName and AfxGetResourceHandle() is. Is there some sort of wierd switch? except for _DEBUG?

                1 Reply Last reply
                0
                • G Gary R Wheeler

                  It sounds like one or more of your DLL's are release compiles. In order for you to debug into them, they all need to be debug builds.

                  Software Zen: delete this;
                  Fold With Us![^]

                  P Offline
                  P Offline
                  panzerdivisionmarkus
                  wrote on last edited by
                  #8

                  finally found something. Seems like this code is causing the assertion:

                  _AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
                  { ASSERT(afxCurrentResourceHandle != NULL);
                  return afxCurrentResourceHandle; }

                  Seems like afxCurrentResourceHandle haven't been properly set. I thought that was what AFX_MANAGE_STATE(AfxGetStaticModuleState()) was doing. You wouldn't happen to know any reason why it would fail?

                  G 1 Reply Last reply
                  0
                  • P panzerdivisionmarkus

                    finally found something. Seems like this code is causing the assertion:

                    _AFXWIN_INLINE HINSTANCE AFXAPI AfxGetResourceHandle()
                    { ASSERT(afxCurrentResourceHandle != NULL);
                    return afxCurrentResourceHandle; }

                    Seems like afxCurrentResourceHandle haven't been properly set. I thought that was what AFX_MANAGE_STATE(AfxGetStaticModuleState()) was doing. You wouldn't happen to know any reason why it would fail?

                    G Offline
                    G Offline
                    Gary R Wheeler
                    wrote on last edited by
                    #9

                    Not off hand, I'm afraid. If you are creating MFC extension DLL's, then that might be a problem. Try looking that up on MSDN.

                    Software Zen: delete this;
                    Fold With Us![^]

                    P 1 Reply Last reply
                    0
                    • G Gary R Wheeler

                      Not off hand, I'm afraid. If you are creating MFC extension DLL's, then that might be a problem. Try looking that up on MSDN.

                      Software Zen: delete this;
                      Fold With Us![^]

                      P Offline
                      P Offline
                      panzerdivisionmarkus
                      wrote on last edited by
                      #10

                      Slolved it. It was a stupid lib file I used that had to be unicode compiled as well. Didn't notice it because the compiler didn't complain. Thanks for your help.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups