WinAPP ExitInstance ExitProcess RRS feed

  • Question

  • Hi , 

    Issue: I have a class WinApp with MMC SnapIn registered OCX , application after launch when closed , there is always a background process running unless I kill it from task Manager 

    I have a overridden ExitInstance() where I unload/freeLibrary of  all the modules , temp directories cleaned, deleting any of allocations 

    solution : after all the process in ExitInstance() which can I invoke ExitProcess(0) to make sure all the threads and process is exited safely , are there any consequences with this ? Is there any other way of closing the application properly ? please share better or right approach to deal with this situation.

    will there be any dangling pointers to after ExitProcess(0) ?       

    PostQuitMessage() is not working                                                                                      




    • Edited by Arathij Wednesday, October 30, 2019 10:57 AM
    Wednesday, October 30, 2019 10:52 AM

All replies

  • I have a overridden ExitInstance() where I unload/freeLibrary of  all the modules , temp directories cleaned, deleting any of allocations

    When your process terminates Windows will automatically unload dlls and there is no need to deallocate memory since the entire process will be destroyed.  Remove these actions from ExitInstance().

    • Edited by RLWA32 Wednesday, October 30, 2019 11:09 AM
    Wednesday, October 30, 2019 11:07 AM
  • It appears you are deadlocking the MMC process by putting cleanup code in ExitInstance.

    For MFC DLL and OCX projects CWinApp::InitInstance and CWinApp::ExitInstance are invoked in DllMain and it is a bad idea to load or unload DLL in the function. See

    Q143084 FIX: Problems with Using the MFC DAO Classes in a .DLL or .OCX

    Q322909 FIX: DLL That Uses ATL CImage Class May Cause a Deadlock in a Process

    MSConnect 1470256 MFC hangs when unloading a regular DLL when using GDI+


    Some reasons not to do anything scary in your DllMain, part 3 on Raymond Chen's blog Old New Thing

    You should clean up before that. You don't really have control over your ActiveX's lifecycle (its controlled by the container) but a common place to clean up is in COleControl::OnSetClientSite. If m_pClientSite is NULL after the base implementation is called, the ActiveX is closing by the parent and you should reduce your static reference counting otherwise the ActiveX is being hosted and you should increase your reference counting. When your reference count reaches 0, you can free your resources.

    For some control containers OnDestroy may be followed by another OnCreate so OnDestroy may not be a good place to cleanup. 

    Visual C++ MVP

    Wednesday, October 30, 2019 4:05 PM
  • will there be any dangling pointers to after ExitProcess(0)

    does this solve the problem of having the process in the background even after closing 


    Monday, November 4, 2019 4:28 AM
  • you don't need to worry about memory after calling ExitProcess. Issue is you should not call it in the first place. Your code in ExitInstance is blocking the hosting process from exiting. 

    Visual C++ MVP

    Monday, November 4, 2019 1:24 PM
  • Thanks for reply 

    This is my overridden ExitInstance() API which is required to free up the memory allocated and loaded modules to be unload 

    after close my process keep running in the background

    when I add exit(0) OR  ExitProcess(0) works fine . no more running background tasks 

    WITHOUT ExitProcess(0) process is not exited still there is a background task even after CLOSE "X"

    int EDMSnapApp::ExitInstance()
    //Clear tmp folder 
    //Clear tmp folder 
    CString strTmpDir;
    DWORD dwRes = ::GetTempPath(255, strTmpDir.GetBuffer(255)); 
    if (0 != dwRes)
    strTmpDir += L"TmpImages";
    CMN::CTempFolder tmpFolder(strTmpDir);
    CString sPath = tmpFolder.GetFolderPath().c_str();
    catch(TException* pEx)

    //  Initializing low-level modules and global vars
    //  save settings to profile
        TWinApp::WriteProfileInt    (ESDC_strSettings, g_cszTryGIT, TComAnpPtrBase::ms_bTryGit);

        TSettingsStorageModeEnum enMode  = TSettings::GetStorageMode();
        TWinApp::WriteProfileInt    (ESDC_strSettings, g_cszSettingsStorageMode, enMode);    
        TWinApp::WriteProfileInt    (ESDC_strSettings, g_cszEncryption, ESDCInterface::ms_bEncryption);    

    TWinApp::WriteProfileInt (ESDC_strSettings, g_cszShowEditAttributesButton, ESWNewWizard97FinishPage::GetShowEditAttributesButton() || ESWNewObjectFinishPage::GetShowEditAttributesButton());

    TWinApp::WriteProfileInt    (ESDC_strSettings, g_cszReportPageSize, ESVReportData::GetReportPageSize());
    TWinApp::WriteProfileInt    (ESDC_strSettings, g_cszReportPageSize2, ESVGMReportData::GetGMReportPageSize());

        // TWinApp::WriteProfileInt    (ESDC_strSettings, g_cszDumpContextDepth,  TGetReleaseDumpContext().GetDepth());
        TWinApp::WriteProfileString (ESDC_strSettings, ESDC_strSchemaCache,     m_strSchemaCachePath);

        // destroy message filter (may be derived class)
        delete m_pMessageFilter;
        m_pMessageFilter = NULL;

        TTrace(traceAll, _T("[EDMSnapApp::ExitInstance]\n\n"));

        int iCode = TWinApp::ExitInstance();

        if (m_pMainWnd)
            delete m_pMainWnd;

    #ifdef _DEBUG



        return iCode;


    • Edited by Arathij Tuesday, November 5, 2019 7:02 AM
    Tuesday, November 5, 2019 7:01 AM
  • Hello,

    If your issue is solved, please "Mark as answer" or "Vote as helpful" post to the appropriate answer , so that it will help other members to find solution quickly if they faces similar issue.

    Best Regards,

    Suarez Zhou

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Tuesday, November 12, 2019 8:57 AM