Question about "failed to create new document!

Source: Internet
Author: User
Analysis of "failed to create empty document!
 
Many new users are always unprepared when they encounter such problems. If anyone is patient, let's take a look at the following article I wrote.
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 );
 
The code above shows that onfilenew and onfileopen of cwinapp call the virtual functions onfilenew and onfileopen of cdocmanager respectively. 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.
 
Look at the actions of the create and createex functions and you will know what is going on.
**************************************** ************************
1. If the menu resource cannot be found, false is returned, and "an empty document fails to be created" is displayed"
Hinstance hinst = afxfindresourcehandle (lpszmenuname, rt_menu );
If (hmenu =: loadmenu (hinst, lpszmenuname) = NULL)
{
Trace0 ("Warning: failed to load menu for cframewnd./N ");
Postncdestroy (); // perhaps Delete the C ++ object
Return false;
}
2. If precreatewindow is overloaded and false is returned, "an empty document fails to be created"
3. If-1 is returned in oncreate, "an empty document fails to be created" will pop up ".
**************************************** **************************
 
The above is the general cause of the "failed to create an empty document" problem I analyzed. There may be other reasons. I will not list them here.

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.