CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2009
    Posts
    166

    AfxRegisterWndClass() causes crash

    Hello,

    My app crashes when I attempt to create a CWnd as shown below. I am attempting to create the CWnd with a a parent of type CFormView. Does anyone know why this might be crashing?

    Code:
    CWnd::Create(AfxRegisterWndClass(CS_DBLCLKS), CString(windowName.c_str()), WS_VISIBLE | WS_BORDER, dimensions, parent, NULL, NULL);
    The call stack looks like this:

    Code:
    mfc100ud.dll!AfxGetInstanceHandle()  Line 21 + 0x20 bytes	C++
    mfc100ud.dll!AfxRegisterWndClass(unsigned int nClassStyle, HICON__ * hCursor, HBRUSH__ * hbrBackground, HICON__ * hIcon)  Line 1462 + 0x5 bytes	C++
    and the line that crashes here us the AfxGetInstanceHandle() call:

    Code:
    LPCTSTR AFXAPI AfxRegisterWndClass(UINT nClassStyle,
    	HCURSOR hCursor, HBRUSH hbrBackground, HICON hIcon)
    {
    	// Returns a temporary string name for the class
    	//  Save in a CString if you want to use it for a long time
    	LPTSTR lpszName = AfxGetThreadState()->m_szTempClassName;
    
    	// generate a synthetic name for this class
    	HINSTANCE hInst = AfxGetInstanceHandle();

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: AfxRegisterWndClass() causes crash

    Where do you call this CWnd::Create from?
    Why do you use CWnd:: prefix?
    Victor Nijegorodov

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: AfxRegisterWndClass() causes crash

    As Victor already suggested, most possible you are calling CWnd::Create... in a wrong way/place, trying to re-create the current window. That asserts in a debug build and may crash in a release one.

    One aside note: why are you using boogie-woogie code like this?
    Code:
    ... CString(windowName.c_str()) ...
    As long as you are using MFC, CString MFC class is enough for handling strings.
    std::string does not help much, just can give you more headaches.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Mar 2009
    Posts
    166

    Re: AfxRegisterWndClass() causes crash

    Ok, i have 3 classes involved in the Create() call. I have the View class, which has a handle to the CWnd derived window. In the view's OnCreate() I have:

    Code:
    int CDisplayView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if(CFormView::OnCreate(lpCreateStruct) == -1)
            return -1;
    
        CRect rect(0,0,0,0);
        windowHandle_.CreateWin(rect, this, "My Window");
        return 0;
    }
    The windowHandle_ is of type CMapWindow, which is derived from CBaseWnd, which is derived from CWnd. The CreateWin() call is in the CBaseWnd class which looks like this:

    Code:
    void BaseWnd::CreateWin(CRect dimension, CWnd* parent, string windowName)
    {
        CWnd::Create(AfxRegisterWndClass(CS_DBLCLKS), CString(windowName.c_str()), WS_VISIBLE | WS_BORDER, dimensions, parent, NULL, NULL);
    }
    Anyone know why this crashes at AfxGetInstanceHandle() ?

    Thanks!

  5. #5
    Join Date
    Mar 2009
    Posts
    166

    Re: AfxRegisterWndClass() causes crash

    I fixed this issue by converting my project to unicode.

  6. #6
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: AfxRegisterWndClass() causes crash

    Quote Originally Posted by ekhule View Post
    I fixed this issue by converting my project to unicode.
    This case it's possible you have some mismatch between UNICODE and ANSI strings. If yes, most possible that's because you are mixing STL in MFC. See my first post.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  7. #7
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: AfxRegisterWndClass() causes crash

    Quote Originally Posted by ekhule View Post
    I fixed this issue by converting my project to unicode.
    I'm very doubt you did. This is not the way how crashes get fixed.
    Best regards,
    Igor

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: AfxRegisterWndClass() causes crash

    Quote Originally Posted by ekhule View Post
    I fixed this issue by converting my project to unicode.
    And how exactly did this fix the issue? If you don't know, then that isn't a fix. You could have had a buffer overrun, and changing the binary executable just moved the bug somewhere else.

    Before doing voodo "fixes", I would suggest you change your code back to the way it was, duplicate the error, and actually figure out why the crash occurs so that a proper fix can be established. Otherwise, you have a program that cannot be proven that it's stable.

    Regards,

    Paul McKenzie

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: AfxRegisterWndClass() causes crash

    Quote Originally Posted by Paul McKenzie View Post
    And how exactly did this fix the issue? If you don't know, then that isn't a fix. You could have had a buffer overrun, and changing the binary executable just moved the bug somewhere else.

    Before doing voodo "fixes", I would suggest you change your code back to the way it was, duplicate the error, and actually figure out why the crash occurs so that a proper fix can be established. Otherwise, you have a program that cannot be proven that it's stable.
    I totally agree that such fixes are not the way to proceed with such problems with c/c++ code. They might temporarily mask the problem but at some stage they will come back 'to bite you hard'. The only way to deal with these problems is with some hard debugging to find the source of the problem and to then change the faulty code appropriately in the knowledge that you know what you are doing and why you are changing the code. Just 'trying a few things' and seeing what happens is not the way to fix any problem!
    Last edited by 2kaud; May 31st, 2013 at 07:09 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  10. #10
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: AfxRegisterWndClass() causes crash

    ...and finally: avoid as much as possible mixing STL in MFC code!
    That prevents a lot of painful headaches.
    No sweat, a trifle, like a walking in the park.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  11. #11
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: AfxRegisterWndClass() causes crash

    ...and an additional remark:
    Once you are using MFC framework, why do you need using the "boogie-woogie architecture" like that described here http://forums.codeguru.com/showthrea...25#post2119225?

    MFC, good or bad as it is, respects the KISS paradigm.
    Any attempt to step beyond, does nothing else but generating headaches.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  12. #12
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: AfxRegisterWndClass() causes crash

    Quote Originally Posted by ovidiucucu View Post
    ...and an additional remark:
    Once you are using MFC framework, why do you need using the "boogie-woogie architecture" like that described here http://forums.codeguru.com/showthrea...25#post2119225?

    MFC, good or bad as it is, respects the KISS paradigm.
    Any attempt to step beyond, does nothing else but generating headaches.
    For an a different opinion, below is from a codeguru post by
    Ronald Laeremans, Visual C++ product unit manager in 2006:

    The MFC collection classes are only there for backwards compatibility.
    C++ has a standard for collection classes and that is the
    Standards C++ Library. There is no technical drawback for using
    any of the standard library in an MFC application.
    See: http://forums.codeguru.com/showthread.php?391319 for full discussion.


    "Further, deponent sayeth naught"

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