Deleting modeless property sheet
-
Hi all, I have a modeless property sheet with three pages. The pages are deleted in PostNCDestroy. However, when a page has not been opened when the sheet is closed, PostNCDestroy for that page is not called, causing memory leaks. How do I solve this? Ronald
-
Hi all, I have a modeless property sheet with three pages. The pages are deleted in PostNCDestroy. However, when a page has not been opened when the sheet is closed, PostNCDestroy for that page is not called, causing memory leaks. How do I solve this? Ronald
rwilmink wrote: ...causing memory leaks. Where? If the page has not been created, it will not have allocated any memory. Is the leak in the property sheet?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
rwilmink wrote: ...causing memory leaks. Where? If the page has not been created, it will not have allocated any memory. Is the leak in the property sheet?
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
The pages are first created on the heap and then added to the sheet (sheet->AddPage(pPage)). I delete the sheet and the pages with 'delete this' in PostNCDestroy. When a page has not been openened (by clicking the tab) PostNCDestroy is not called so the page is not deleted from the heap.
-
The pages are first created on the heap and then added to the sheet (sheet->AddPage(pPage)). I delete the sheet and the pages with 'delete this' in PostNCDestroy. When a page has not been openened (by clicking the tab) PostNCDestroy is not called so the page is not deleted from the heap.
rwilmink wrote: The pages are first created on the heap... Why? Can you not just use something like:
class MySheet : CPropertySheet
{
CMyPage page1;
CMyPage page2;
CMyPage page3;
};MySheet::MySheet()
{
AddPage(&page1);
AddPage(&page1);
AddPage(&page1);
}No heap and no memory allocation/cleanup to mess with.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
rwilmink wrote: The pages are first created on the heap... Why? Can you not just use something like:
class MySheet : CPropertySheet
{
CMyPage page1;
CMyPage page2;
CMyPage page3;
};MySheet::MySheet()
{
AddPage(&page1);
AddPage(&page1);
AddPage(&page1);
}No heap and no memory allocation/cleanup to mess with.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb
-
Ok, then if the property sheet is allocating memory for the pages, it should also free up that memory. Something like:
class MySheet : public CPropertySheet
{
MyPage *page1;
MyPage *page2;
MyPage *page3;
};MySheet::MySheet()
{
page1 = new MyPage;
AddPage(page1);
page2 = new MyPage;
AddPage(page2);
page3 = new MyPage;
AddPage(page3);
}MySheet::~MySheet()
{
delete page1;
delete page2;
delete page3;
}BOOL MySheet::OnCommand( WPARAM wParam, LPARAM lParam )
{
WORD wNotifyCode,
wId;wId = LOWORD(wParam); wNotifyCode = HIWORD(wParam); if (BN\_CLICKED == wNotifyCode) { if (IDOK == wId) { if (! ProcOk()) return TRUE; } } return CPropertySheet::OnCommand(wParam, lParam);
}
// the OK button was clicked
BOOL MySheet::ProcOk( void )
{
if (::IsWindow(page1->m_hWnd))
{
if (! page1->OnKillActive())
return FALSE;
}if (::IsWindow(page1->m\_hWnd)) page1->OnOK(); return TRUE;
}
void MyPage::OnOK()
{
// the sheet is about to go away.
// do any page-specific cleanup hereCPropertyPage::OnOK();
}
BOOL MyPage::OnKillActive()
{
// the page is being changed.
// do any page-specific validation here.
// return 0 if the page should not changereturn CPropertyPage::OnKillActive();
}
"One must learn from the bite of the fire to leave it alone." - Native American Proverb