|
From: Nick Schultz on 2 May 2008 13:59 Hi there, My user will need to be able to open any number of graphing windows(CDockablePane) in my application. When the window is closed, I want the pane to be destroyed. In order to do this I will have my window catch the press close button event (CPane::OnPressCloseButton) and then have it call DestroyWindow(). reading DestroyWindow description, it looks like it does everything but it does not destroy the CWnd object. So how do I destroy the CWnd object? Do i just simply need to call delete (pointer to graph window). I was thinking I could override the NotifyParent function to catch child destroyed event and then deleting it then, however the parent gets notified BEFORE any destruction takes place. I'm not sure if this is possible, but after calling DestroyWindow() within the OnPressCloseButton() function, could I just call "delete this" to free up any memory used by the graph window? Any suggestions are greatly appreciated. Nick
From: Joseph M. Newcomer on 2 May 2008 14:30 If you are allocating them on the heap, with new, the easiest way is to add a PostNcDestroy handler to your class whose body says delete this; replacing the TODO: line. You cannot delete an object earlier than the PostNcDestroy handler. joe On Fri, 2 May 2008 10:59:01 -0700, Nick Schultz <NickSchultz(a)discussions.microsoft.com> wrote: >Hi there, > >My user will need to be able to open any number of graphing >windows(CDockablePane) in my application. When the window is closed, I want >the pane to be destroyed. > >In order to do this I will have my window catch the press close button event >(CPane::OnPressCloseButton) and then have it call DestroyWindow(). > >reading DestroyWindow description, it looks like it does everything but it >does not destroy the CWnd object. > >So how do I destroy the CWnd object? Do i just simply need to call delete >(pointer to graph window). I was thinking I could override the NotifyParent >function to catch child destroyed event and then deleting it then, however >the parent gets notified BEFORE any destruction takes place. > >I'm not sure if this is possible, but after calling DestroyWindow() within >the OnPressCloseButton() function, could I just call "delete this" to free >up any memory used by the graph window? > >Any suggestions are greatly appreciated. > >Nick Joseph M. Newcomer [MVP] email: newcomer(a)flounder.com Web: http://www.flounder.com MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Nick Schultz on 2 May 2008 17:03 OK, I tried overriding both PostNcDestroy and OnPressCloseButton functions and it still isn't working the way I would like it to... When I create the window, it starts out docked to the main frame. If I undock the window and then close it, those functions do not get called. If I leave it docked, or undock it and dock it again and then close the window, those functions get called. After going through those two functions, I then get an Access violation: Unhandled exception at 0x78a75e55 (mfc90ud.dll) in CanAnalyzer.exe: 0xC0000005: Access violation reading location 0xfeef000e. and the break point is at the return statement: LRESULT CWnd::Default() { // call DefWindowProc with the last message _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData(); return DefWindowProc(pThreadState->m_lastSentMsg.message, pThreadState->m_lastSentMsg.wParam, pThreadState->m_lastSentMsg.lParam); } Here is the code of the closing functions: void CGrapher::OnPressCloseButton() { DestroyWindow(); } void CGrapher::PostNcDestroy(){ delete this; } So i guess I got a couple questions now, why am i getting that access violation and why wont those functions get called when the CDockablePane is undocked(floating)? Thanks for the help, Nick "Joseph M. Newcomer" wrote: > If you are allocating them on the heap, with new, the easiest way is to add a > PostNcDestroy handler to your class whose body says > delete this; > replacing the TODO: line. > > You cannot delete an object earlier than the PostNcDestroy handler. > joe > > On Fri, 2 May 2008 10:59:01 -0700, Nick Schultz <NickSchultz(a)discussions.microsoft.com> > wrote: > > >Hi there, > > > >My user will need to be able to open any number of graphing > >windows(CDockablePane) in my application. When the window is closed, I want > >the pane to be destroyed. > > > >In order to do this I will have my window catch the press close button event > >(CPane::OnPressCloseButton) and then have it call DestroyWindow(). > > > >reading DestroyWindow description, it looks like it does everything but it > >does not destroy the CWnd object. > > > >So how do I destroy the CWnd object? Do i just simply need to call delete > >(pointer to graph window). I was thinking I could override the NotifyParent > >function to catch child destroyed event and then deleting it then, however > >the parent gets notified BEFORE any destruction takes place. > > > >I'm not sure if this is possible, but after calling DestroyWindow() within > >the OnPressCloseButton() function, could I just call "delete this" to free > >up any memory used by the graph window? > > > >Any suggestions are greatly appreciated. > > > >Nick > Joseph M. Newcomer [MVP] > email: newcomer(a)flounder.com > Web: http://www.flounder.com > MVP Tips: http://www.flounder.com/mvp_tips.htm >
From: Nick Schultz on 2 May 2008 17:05 Here is the code for the button that creates the window: void CMainFrame::OnRmtSend2Btn() { CGrapher* newWnd; newWnd = new CGrapher(); if(!newWnd->Create(_T("Graph Window"), this, CRect(0, 0, 300, 300), TRUE ,m_windowCounter++, WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN | CBRS_RIGHT | CBRS_FLOAT_MULTI)) { TRACE0("Failed to create BITWnd window\n"); return; // failed to create } newWnd->EnableDocking(CBRS_ALIGN_ANY); DockPane(newWnd); return; } "Joseph M. Newcomer" wrote: > If you are allocating them on the heap, with new, the easiest way is to add a > PostNcDestroy handler to your class whose body says > delete this; > replacing the TODO: line. > > You cannot delete an object earlier than the PostNcDestroy handler. > joe > > On Fri, 2 May 2008 10:59:01 -0700, Nick Schultz <NickSchultz(a)discussions.microsoft.com> > wrote: > > >Hi there, > > > >My user will need to be able to open any number of graphing > >windows(CDockablePane) in my application. When the window is closed, I want > >the pane to be destroyed. > > > >In order to do this I will have my window catch the press close button event > >(CPane::OnPressCloseButton) and then have it call DestroyWindow(). > > > >reading DestroyWindow description, it looks like it does everything but it > >does not destroy the CWnd object. > > > >So how do I destroy the CWnd object? Do i just simply need to call delete > >(pointer to graph window). I was thinking I could override the NotifyParent > >function to catch child destroyed event and then deleting it then, however > >the parent gets notified BEFORE any destruction takes place. > > > >I'm not sure if this is possible, but after calling DestroyWindow() within > >the OnPressCloseButton() function, could I just call "delete this" to free > >up any memory used by the graph window? > > > >Any suggestions are greatly appreciated. > > > >Nick > Joseph M. Newcomer [MVP] > email: newcomer(a)flounder.com > Web: http://www.flounder.com > MVP Tips: http://www.flounder.com/mvp_tips.htm >
From: Ajay Kalra on 2 May 2008 17:28
On May 2, 5:03 pm, Nick Schultz <NickSchu...(a)discussions.microsoft.com> wrote: > OK, I tried overriding both PostNcDestroy and OnPressCloseButton functions > and it still isn't working the way I would like it to... > > When I create the window, it starts out docked to the main frame. If I > undock the window and then close it, those functions do not get called. If I > leave it docked, or undock it and dock it again and then close the window, > those functions get called. > > After going through those two functions, I then get an Access violation: > > Unhandled exception at 0x78a75e55 (mfc90ud.dll) in CanAnalyzer.exe: > 0xC0000005: Access violation reading location 0xfeef000e. > > and the break point is at the return statement: > > LRESULT CWnd::Default() > { > // call DefWindowProc with the last message > _AFX_THREAD_STATE* pThreadState = _afxThreadState.GetData(); > return DefWindowProc(pThreadState->m_lastSentMsg.message, > pThreadState->m_lastSentMsg.wParam, pThreadState->m_lastSentMsg.lParam); > > } > > Here is the code of the closing functions: > > void CGrapher::OnPressCloseButton() > { > DestroyWindow(); } > > void CGrapher::PostNcDestroy(){ > delete this; > > } > > So i guess I got a couple questions now, why am i getting that access > violation and why wont those functions get called when the CDockablePane is > undocked(floating)? > > Thanks for the help, > > Nick > > "Joseph M. Newcomer" wrote: > > If you are allocating them on the heap, with new, the easiest way is to add a > > PostNcDestroy handler to your class whose body says > > delete this; > > replacing the TODO: line. > > > You cannot delete an object earlier than the PostNcDestroy handler. > > joe > > > On Fri, 2 May 2008 10:59:01 -0700, Nick Schultz <NickSchu...(a)discussions.microsoft.com> > > wrote: > > > >Hi there, > > > >My user will need to be able to open any number of graphing > > >windows(CDockablePane) in my application. When the window is closed, I want > > >the pane to be destroyed. > > > >In order to do this I will have my window catch the press close button event > > >(CPane::OnPressCloseButton) and then have it call DestroyWindow(). > > > >reading DestroyWindow description, it looks like it does everything but it > > >does not destroy the CWnd object. > > > >So how do I destroy the CWnd object? Do i just simply need to call delete > > >(pointer to graph window). I was thinking I could override the NotifyParent > > >function to catch child destroyed event and then deleting it then, however > > >the parent gets notified BEFORE any destruction takes place. > > > >I'm not sure if this is possible, but after calling DestroyWindow() within > > >the OnPressCloseButton() function, could I just call "delete this" to free > > >up any memory used by the graph window? > > > >Any suggestions are greatly appreciated. > > > >Nick > > Joseph M. Newcomer [MVP] > > email: newco...(a)flounder.com > > Web:http://www.flounder.com > > MVP Tips:http://www.flounder.com/mvp_tips.htm Dont call DestroyWindow; use PostMessage(WM_CLOSE) instead. See if that helps. -- Ajay |