调用CFrameWnd :: SetActiveView()会使我的应用程序崩溃... [英] A call to CFrameWnd::SetActiveView() makes my app crash at close...
问题描述
我使用Doc / View架构和VS2015构建了一个MFC应用程序。
I build a MFC application with Doc/View architecture and VS2015.
为了使加速键到达良好的视图,我得出结论我必须调用CFrameWnd :: SetActiveView (...)并且它工作正常。
To make accelerator keys arrive to the good view, I concluded that I must call CFrameWnd::SetActiveView(...) and it works fine.
但在这种情况下,我的应用程序在关闭时崩溃。
But in this case, my application crash when closing.
调查,我发现在CFrameWnd :: OnClose()中发生异常,并且,读取代码,我认为存在问题。
Investigating, I found that the exception occures in CFrameWnd::OnClose() and, reading the code, I think there is a problem.
示意图,CFrameWnd :: OnClose()代码是(我只显示有趣的代码行):
Schematically, the CFrameWnd::OnClose() code is (I show only interesting lines of code) :
CDocument* pDocument = GetActiveDocument();
...
pApp-> CloseAllDocuments(FALSE);
pApp->CloseAllDocuments(FALSE);
...
   if(pDocument!= NULL&& pDocument-> m_bAutoDelete)
  {
       BOOL bOtherFrame = FALSE;
    POSITION pos = pDocument-> GetFirstViewPosition();
...
if (pDocument != NULL && pDocument->m_bAutoDelete)
{
BOOL bOtherFrame = FALSE;
POSITION pos = pDocument->GetFirstViewPosition();
...
如果调用CFrameWnd :: SetActiveView() ,GetActiveDocument()返回一个非空指针指向文档。
If a call to CFrameWnd::SetActiveView() is made, GetActiveDocument() return a non null pointer to a document.
但是这个文档在CloseAllDocuments()中删除,而pDocument指向一个解除分配的内存区域。
最后一段代码会产生访问冲突。
But this document is deleted in CloseAllDocuments() and pDocument is pointing on a desallocated memory area.
The last piece of code generate an access violation.
我做错了什么?
SetActiveView()有什么好的做法?
CFrameWnd :: OnClose()中是否有错误?
What am I doing wrong?
What is the good practice with SetActiveView()?
Is there a bug in CFrameWnd::OnClose()?
提前致谢
Ludovic
推荐答案
Hi Menuki,
Hi Menuki,
感谢您在此处发帖。
>>我做错了什么?
如果您能获得有关您所执行步骤的更多信息,那将会更好。你怎么以及何时打电话给CFrameWnd :: SetActiveView?你做了什么操作员进入CFrameWnd :: OnClose()?这样我们就可以重现您的问题并找到根本原因。
It will be better if you could more information about the steps you did. How and when do you call CFrameWnd::SetActiveView? What operator did you do to step into CFrameWnd::OnClose() ? So that we could reproduce your problem and find the root cause.
>> SetActiveView()有什么好的做法?
通常,框架会在用户将焦点更改为框架窗口内的视图时自动调用此函数。您可以明确调用SetActiveView将焦点更改为指定的视图。
Normally the framework will call this function automatically as the user changes the focus to a view within the frame window. You can explicitly call SetActiveView to change the focus to the specified view.
>> CFrameWnd中是否存在错误:: OnClose()?
实际上,如果你查看代码,你会发现这个。
Actually if you look into the code, you will find this.
// Note: only queries the active document
CDocument* pDocument = GetActiveDocument();
if (pDocument != NULL && !pDocument->CanCloseFrame(this))
{
......
// close all documents first
......
}
// detect the case that this is the last frame on the document and
// shut down with OnCloseDocument instead.
if (pDocument != NULL && pDocument->m_bAutoDelete)
{
......
}
你所说的关于CloseAllDocuments()和pDocument的条件是两个if语句。
The condition you said about CloseAllDocuments() and pDocument are in two if statements.
最好的问候,
Sera Yu
Best Regards,
Sera Yu
这篇关于调用CFrameWnd :: SetActiveView()会使我的应用程序崩溃...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!