CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: App(mfc) failing in Release build due to compiler optimization but works in debug

  1. #1
    Join Date
    Nov 2015
    Posts
    1

    App(mfc) failing in Release build due to compiler optimization but works in debug

    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.

    Code:
    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

    Code:
    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.
    I am using legacy code in Visual studio 2005

    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

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: App(mfc) failing in Release build due to compiler optimization but works in debug

    DO NOT TRUST in what you see when step into a RELEASE build!
    Because of code optimization, wrong values may be shown.
    If you want to trace code also in RELEASE, use a logging system (e.g. write values in a log file) or call OutputDebugString function then look into Output window.
    Anyway, turning off the compiler optimization in RELEASE is NOT a good solution.

    One additional note: if use OutputDebugString, you can also trace the values in Sysinternals DebugView utility without being necessary to run your application from Visual Studio IDE.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  3. #3

    Re: App(mfc) failing in Release build due to compiler optimization but works in debug

    DebugView is an application that lets you monitor debug output on your local system, or any computer on the network that you can reach via TCP/IP. It is capable of displaying both kernel-mode and Win32 debug output, so you don't need a debugger to catch the debug output your applications or device drivers generate, nor do you need to modify your applications or drivers to use non-standard debug output APIs.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)