Analysis on "failed to create empty document" in MFC

Source: Internet
Author: User

This type of problem occurs mainly in bool cwinapp: processshellcommand (ccommandlineinfo & rcmdinfo );

Key functions:
Bool bresult = true;
Switch (rcmdinfo. m_nshellcommand)
{
Case ccommandlineinfo: filenew: // create
If (! Afxgetapp ()-> on1_msg (id_file_new, 0, null, null ))
Onfilenew ();
If (m_pmainwnd = NULL)
Bresult = false;
Break;
Case ccommandlineinfo: fileopen:
If (! Opendocumentfile (rcmdinfo. m_strfilename ))
Bresult = false;
Break;
From the above content, we can see that if there is no IDing to id_file_new, the problem occurs in onfilenew ();
Cwinapp's default Implementation of onfilenew is to call m_pdocmanager-> onfilenew ();

Let's continue to parse cdocmanager. What exactly does it do?
(The main function of cdocmanager is to help cwinapp manage the linked list of document templates and register file types .)

// If the template list is empty
If (m_templatelist.isempty ())
{
Trace0 ("error: No document templates registered with cwinapp. \ n ");
Afxmessagebox (afx_idp_failed_to_create_doc); // an error is returned and no new document error is reported.
Return;
}

Cdoctemplate * ptemplate = (cdoctemplate *) m_templatelist.gethead ();
If (m_templatelist.getcount ()> 1)
{
// More than one document template to choose from
// Bring up dialog prompting user
Cnewtypedlg DLG (& m_templatelist );
Int nid = DLG. domodal ();
If (nid = idok)
Ptemplate = DLG. m_pselectedtemplate;
Else
Return; // none-cancel Operation
}

Assert (ptemplate! = NULL );
Assert_kindof (cdoctemplate, ptemplate );

Ptemplate-> opendocumentfile (null );

Through the aboveCodeWe can see that onfilenew and onfileopen of cwinapp respectively call the virtual function onfilenew of cdocmanager.

And onfileopen. In cdocmanager. Use the template linked list to select different templates to call the opendocumentfile () of the document template ();
If the input parameter is null, the file is created.

Next let's take a look at cdoctemplate: opendocumentfile (), which is a key function. Because it is a virtual function, we consider
Csingledoctemplate: opendocumentfile.
There is a piece of code in this function:
Afx_idp_failed_to_create_doc indicates the resource ID of the character "failed to create an empty document ".
// Create a new document
Pdocument = createnewdocument ();
Assert (pframe = NULL); // will be created below
Bcreated = true;
If (pdocument = NULL)
{
Afxmessagebox (afx_idp_failed_to_create_doc );
Return NULL;
}
Assert (pdocument = m_ponlydoc );
If (pframe = NULL)
{
Assert (bcreated );

// Create frame-set as main document frame
Bool bautodelete = pdocument-> m_bautodelete;
Pdocument-> m_bautodelete = false;
// Don't destroy if something goes wrong
Pframe = createnewframe (pdocument, null );
Pdocument-> m_bautodelete = bautodelete;
If (pframe = NULL)
{
Afxmessagebox (afx_idp_failed_to_create_doc );
Delete pdocument; // explicit delete on Error
Return NULL;
}

By observing the code above, we can easily see two possible causes of errors: 1 createnewdocument returns NULL 2

The return value of createnewframe is null.

First look at createnewdocument (). Generally, this function rarely fails. However, you cannot take it lightly during debugging.
Let's take a look at createnewframe (). The loadframe function is the source of the error "failed to create a new document.
If it returns false, this prompt is displayed.
Let's take a look at loadframe () and call cframewnd: Create () to create a window. If the window fails to be created, fasle is returned.
This makes the problem easier.

check the action of the create and createex functions to see what is going on.
************************************* * *************************
1. If the menu resource cannot be found, false is returned. an error occurred while creating an empty document.
hinstance hinst = afxfindresourcehandle (lpszmenuname, rt_menu);
If (hmenu =: loadmenu (hinst, lpszmenuname) = NULL)
{< br> trace0 ("Warning: failed to load menu for cframewnd. \ n ");
postncdestroy (); // perhaps Delete the C ++ object
return false;
}< br> 2 overload precreatewindow and return If this parameter is set to false, "null document creation failed" is displayed.
3. If-1 is returned in oncreate, "null document creation failed" is displayed ".
************************************* ****************************

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.