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

    Question how CWinApp realize the singleton pattern?

    Generally,singleton pattern is applied by the way that the ctor is private and gives a member funtion named getInstance, but CWinApp's instance is defined as a global object,its ctor is not private.So how MFC realizes the singleton pattern for CWinApp?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: how CWinApp realize the singleton pattern?

    It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:

    Code:
    CWinApp::CWinApp(LPCTSTR lpszAppName)
    {
        if (lpszAppName != NULL)
            m_pszAppName = _tcsdup(lpszAppName);
        else
            m_pszAppName = NULL;
    
        // initialize CWinThread state
        AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
        ENSURE(pModuleState);
        AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
        ENSURE(pThreadState);
        ASSERT(AfxGetThread() == NULL);   // <- this assertion fails
       ...
    }
    So, the first CWinApp instance constructor makes some initialization, setting global variables. The second instance asserts that these variables are not initialized. Access to the only CWinApp-derived instance is done using AFX_MODULE_STATE global variable.
    Last edited by Alex F; July 22nd, 2010 at 12:41 AM.

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: how CWinApp realize the singleton pattern?

    MFC was around long before design patterns such as the Singleton became mainstream.

  4. #4
    Join Date
    Jul 2010
    Posts
    5

    Re: how CWinApp realize the singleton pattern?

    Quote Originally Posted by Alex F View Post
    It doesn't implement classis singleton pattern. Try to create second CWinApp-derived object - the program is compiled, but gives assertion when executed:

    Code:
    CWinApp::CWinApp(LPCTSTR lpszAppName)
    {
        if (lpszAppName != NULL)
            m_pszAppName = _tcsdup(lpszAppName);
        else
            m_pszAppName = NULL;
    
        // initialize CWinThread state
        AFX_MODULE_STATE* pModuleState = _AFX_CMDTARGET_GETSTATE();
        ENSURE(pModuleState);
        AFX_MODULE_THREAD_STATE* pThreadState = pModuleState->m_thread;
        ENSURE(pThreadState);
        ASSERT(AfxGetThread() == NULL);   // <- this assertion fails
       ...
    }
    So, the first CWinApp instance constructor makes some initialization, setting global variables. The second instance asserts that these variables are not initialized. Access to the only CWinApp-derived instance is done using AFX_MODULE_STATE global variable.
    thanks for your reply!
    In debug mode, a assert will be throwed.Ignoring the assert,the application would continue to execute well. If creating second CWinApp-derived object in release mode, there is no assert.Does the second instance overwrite the first instance?Is it that CWinApp does not strictly apply singleton pattern?
    Last edited by zxjun84; July 22nd, 2010 at 07:35 AM.

  5. #5
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: how CWinApp realize the singleton pattern?

    Quote Originally Posted by zxjun84 View Post
    But why T. Kulathu Sarma in his article "MFC and Design Patterns" involved that "CWinApp and its descendants are called Singleton Classes"?The link is :http://www.codeproject.com/KB/archit...cpatterns.aspx
    Strictly speaking, a singleton is a class of which there can be at most one instance. However, sometimes the name is also used for classes of which there should be at most one instance.
    Note that even if there can be at most one instance, it still isn't specified how that is enforced. It may be at compile time (preferably), or it may be at run time.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  6. #6
    Join Date
    Jul 2002
    Posts
    2,543

    Re: how CWinApp realize the singleton pattern?

    I have no idea, what happens in Release mode, or in Debug mode, if assertion is ignored. Undefined behavior or exception. It is not difficult to test.

  7. #7
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: how CWinApp realize the singleton pattern?

    Is it that CWinApp does not strictly apply singleton pattern?
    As Alex and Arjay said, CWinApp does not implement the singleton pattern. It bears the requirement that you don't create a second instance. Period.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

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
  •  





Click Here to Expand Forum to Full Width

Featured