View in Dialog Problems
-
I am writing codes to put a view in a dialog. Here are the code to initialize the view in a dialog:
BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CCreateContext pContext; CWnd* pFrameWnd = this; pContext.m_pCurrentDoc = new CMyDoc(); pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView); CMyView *pView = (CMyView *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext); ASSERT(pView); pView->ShowWindow(SW_NORMAL); /* * After a view is created, resize that to * have the same size as the dialog. */ CRect rectWindow; GetWindowRect(rectWindow); ScreenToClient(rectWindow); /** * Leave a little space for border and title... */ rectWindow.right += 15; rectWindow.top -= 10; pView->MoveWindow(rectWindow); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
I got two problems: (1) If the CMyView is kind of CHtmlView and if the dialog is displayed within an MFC MDI application, when I clicked the view right after the view is displayed, I got an ASSERT in CView::OnMouseActivate(..). But if the dialog is the main window of a dialog-based MFC app, it works fine. Why?:confused: (2) I use the following code to detect if there is memory leak:void CMyDlgView::OnViewViewInDialog() { // TODO: Add your command handler code here // Declare the variables needed #ifdef _DEBUG CMemoryState oldMemState, newMemState, diffMemState; oldMemState.Checkpoint(); #endif ShowDialog(); #ifdef _DEBUG newMemState.Checkpoint(); if( diffMemState.Difference( oldMemState, newMemState ) ) { TRACE( "Memory leaked!\n" ); } #endif } void CMyDlgView::ShowDialog() { CMyDialog dlg; dlg.DoModal(); }
if it is a CHtmlView derived view, there is memork leak; however if it is a generic CView derived view, no memory leak. Why?:confused: -
I am writing codes to put a view in a dialog. Here are the code to initialize the view in a dialog:
BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CCreateContext pContext; CWnd* pFrameWnd = this; pContext.m_pCurrentDoc = new CMyDoc(); pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView); CMyView *pView = (CMyView *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext); ASSERT(pView); pView->ShowWindow(SW_NORMAL); /* * After a view is created, resize that to * have the same size as the dialog. */ CRect rectWindow; GetWindowRect(rectWindow); ScreenToClient(rectWindow); /** * Leave a little space for border and title... */ rectWindow.right += 15; rectWindow.top -= 10; pView->MoveWindow(rectWindow); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
I got two problems: (1) If the CMyView is kind of CHtmlView and if the dialog is displayed within an MFC MDI application, when I clicked the view right after the view is displayed, I got an ASSERT in CView::OnMouseActivate(..). But if the dialog is the main window of a dialog-based MFC app, it works fine. Why?:confused: (2) I use the following code to detect if there is memory leak:void CMyDlgView::OnViewViewInDialog() { // TODO: Add your command handler code here // Declare the variables needed #ifdef _DEBUG CMemoryState oldMemState, newMemState, diffMemState; oldMemState.Checkpoint(); #endif ShowDialog(); #ifdef _DEBUG newMemState.Checkpoint(); if( diffMemState.Difference( oldMemState, newMemState ) ) { TRACE( "Memory leaked!\n" ); } #endif } void CMyDlgView::ShowDialog() { CMyDialog dlg; dlg.DoModal(); }
if it is a CHtmlView derived view, there is memork leak; however if it is a generic CView derived view, no memory leak. Why?:confused:It is this that got me thinking:
((CFrameWnd*)pFrameWnd)->CreateView(...)
If I'm understading your code,
pFrameWnd
is aCDialog
and not aCFrameWnd
. My guess it that this works (when it does) due to pure chance, and it's no wonder weird things happen afterward. Maybe you can take a look at the source code ofCFrameWnd::CreateView
to try to determine what's going wrong. Joaquín M López Muñoz Telefónica, Investigación y Desarrollo -
I am writing codes to put a view in a dialog. Here are the code to initialize the view in a dialog:
BOOL CMyDialog::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here CCreateContext pContext; CWnd* pFrameWnd = this; pContext.m_pCurrentDoc = new CMyDoc(); pContext.m_pNewViewClass = RUNTIME_CLASS(CMyView); CMyView *pView = (CMyView *) ((CFrameWnd*)pFrameWnd)->CreateView(&pContext); ASSERT(pView); pView->ShowWindow(SW_NORMAL); /* * After a view is created, resize that to * have the same size as the dialog. */ CRect rectWindow; GetWindowRect(rectWindow); ScreenToClient(rectWindow); /** * Leave a little space for border and title... */ rectWindow.right += 15; rectWindow.top -= 10; pView->MoveWindow(rectWindow); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE }
I got two problems: (1) If the CMyView is kind of CHtmlView and if the dialog is displayed within an MFC MDI application, when I clicked the view right after the view is displayed, I got an ASSERT in CView::OnMouseActivate(..). But if the dialog is the main window of a dialog-based MFC app, it works fine. Why?:confused: (2) I use the following code to detect if there is memory leak:void CMyDlgView::OnViewViewInDialog() { // TODO: Add your command handler code here // Declare the variables needed #ifdef _DEBUG CMemoryState oldMemState, newMemState, diffMemState; oldMemState.Checkpoint(); #endif ShowDialog(); #ifdef _DEBUG newMemState.Checkpoint(); if( diffMemState.Difference( oldMemState, newMemState ) ) { TRACE( "Memory leaked!\n" ); } #endif } void CMyDlgView::ShowDialog() { CMyDialog dlg; dlg.DoModal(); }
if it is a CHtmlView derived view, there is memork leak; however if it is a generic CView derived view, no memory leak. Why?:confused:One of the problems I've come across when using the CMemoryState items is that they tend to take an exact snapshot of memory. Good for some things, bad for others. It showed a memory leak in my code that was really just the fact that I took the snapshot before initializing an array and then comparing it to the array after it was freed and set to empty at the end of the function. The 'leak' went away if I took the snapshot after I did a memset to initialize the array instead of before. My guess is that the class initializes some structures and the difference is shown as a leak. If you use a trial version of one of the other memory checkers (BoundsChecker, Insure, etc...) then it might help determine if it's a real leak. Good luck with the project.
-
One of the problems I've come across when using the CMemoryState items is that they tend to take an exact snapshot of memory. Good for some things, bad for others. It showed a memory leak in my code that was really just the fact that I took the snapshot before initializing an array and then comparing it to the array after it was freed and set to empty at the end of the function. The 'leak' went away if I took the snapshot after I did a memset to initialize the array instead of before. My guess is that the class initializes some structures and the difference is shown as a leak. If you use a trial version of one of the other memory checkers (BoundsChecker, Insure, etc...) then it might help determine if it's a real leak. Good luck with the project.
Anonymous wrote: My guess is that the class initializes some structures and the difference is shown as a leak. If you use a trial version of one of the other memory checkers (BoundsChecker, Insure, etc...) then it might help determine if it's a real leak. Thanks a lot. Your argument is what I have been suspecting. I would try other memory check programs for this example. Hope you are right.
-
It is this that got me thinking:
((CFrameWnd*)pFrameWnd)->CreateView(...)
If I'm understading your code,
pFrameWnd
is aCDialog
and not aCFrameWnd
. My guess it that this works (when it does) due to pure chance, and it's no wonder weird things happen afterward. Maybe you can take a look at the source code ofCFrameWnd::CreateView
to try to determine what's going wrong. Joaquín M López Muñoz Telefónica, Investigación y DesarrolloI have tried other method to create a view using something like: CView *pView = pRuntimeClass->CreateObject(); pView->Create(...); It got the same assert. However if I add (Overide) a message handler of OnMouseActive(..)of CMyView and just call CWnd::OnMouseActivate(..) within that handler, the ASSERT goes away. Odd!