none
Application(mfc) failing in Release build due to compiler optimization but working fine in debug build RRS feed

  • Question

  • I am facing issue with Release build. Application works fine in Debug build but in release build a pointer initialized to hold object of another class is getting different address allocation and thus causing corruption to its values.

    My main application class is K32App
    code in K32App.h file 
    CSheetPrintManager* m_pSheetPrintManager;
    CSheetPrintManager* GetSheetPrintManager() { return m_pSheetPrintManager; }
    
    In file K32App.cpp
    K32App::K32App()
    {
      m_pSheetPrintManager= NULL;
    } 
    BOOL K32App::InitInstance()
    {
      if(!m_pSheetPrintManager)
        m_pSheetPrintManager= new CSheetPrintManager();
    }
    K32App::~K32App()
    {
      if(m_pSheetPrintManager)
        delete(m_pSheetPrintManager)
    }
    
    
     In my file  CSheetPrintManager.cpp
     void CSheetPrintManager::CSheetPrintManager()
     {
       //Initialized all member variables to default values.
       Init();
    
     }
     void CSheetPrintManager::Init()
     {
       m_nSheetType = SheetIllegalNone;  //long
       m_sBankEntry.Empty();         //CString
       m_bHistorical = FALSE;        //BOOL
       m_bDebitDetailsSet = FALSE;  //BOOL
       m_mapRequested.RemoveAll(); // Type CMap<long,long,CString,CString&> 
     }

    During application startup, when it reaches

      if(!m_pSheetPrintManager)
        CSheetPrintManager= new CSheetPrintManager();

    and tries to create object of m_pSheetPrintManager, 'this' pointer inside CSheetPrintManager.cpp shows valid address (0x03768ce0) at breakpoint just at curly brace {, once I step further into CSheetPrintManager.Init(), 'this' gets different location and starts point to another address(0x0000000) and then further moving it starts pointing another location(0x03786ce0) and then on reaching m_mapRequested.RemoveAll(); 'this' is pointing some other location. returning back to main app file C32App.cpp , I get following for 'm_pSheetPrintManager' CXX0030 Error 'expression cannot be evaluated" in Auto window. and appplication continues to run. See what get when hover mouse of m_pSheetPrintManager (can't post image because need 10 reputations for it :) so linking it)studio Auto window screenshot

    In debug mode, I get m_pSheetPrintManager pointing to same location during all application processing and members always remain properly initialized.

    But in Release mode, m_pSheetPrintManager continues to point (address value shown in Auto window) different location. and all member variables of class CSheetPrintManager showing different garbage(Uninitialized) values with each line of processing inside CSheetPrintManager Class.

    If I disable C++ compiler optimization in Release-Mode then it works fine without any issue.

    Any help/guidance/suggestion is most appreciated. Thanks in advance.

    PS: This is my first question here so please excuse in case missing something to point or express properly.

    Monday, November 9, 2015 1:05 PM

Answers

All replies