-
October 3rd, 2013, 04:42 PM
#1
MFC PRINTDLG release memory issue
Hi,
If I do below code at the bottom of this message without the global free over and over then more and more memory is used without been free'd.
now, if i try and globalfree like below then the memory stays the same... which is good.
but the 2nd run I call it crashes with access violation. looking at the debug the lpDevNames is null:-
void CWinApp::UpdatePrinterSelection(BOOL bForceDefaults)
{
if (!bForceDefaults && m_hDevNames != NULL)
{
LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
ASSERT(lpDevNames != NULL);
does anyone have any ideas how to correct this? I have tried unlocking and creating a new instance e.g. PRINTDLG *pd = new PRINTDLG; to no avail.
----------------
void CTestprintdlg_memoryDlg::OnButton1()
{
PRINTDLG pd;
pd.lStructSize = sizeof( pd );
// Get MFC's printer
if(AfxGetApp()->GetPrinterDeviceDefaults(&pd) )
{
if (pd.hDevMode != NULL) GlobalFree(pd.hDevMode);
if (pd.hDevNames != NULL) GlobalFree(pd.hDevNames);
AfxMessageBox("ok");
}
}
-
October 4th, 2013, 03:28 AM
#2
Re: MFC PRINTDLG release memory issue
Originally Posted by hobnob
Hi,
If I do below code at the bottom of this message without the global free over and over then more and more memory is used without been free'd.
now, if i try and globalfree like below then the memory stays the same... which is good.
but the 2nd run I call it crashes with access violation. looking at the debug the lpDevNames is null:-
Code:
void CWinApp::UpdatePrinterSelection(BOOL bForceDefaults)
{
if (!bForceDefaults && m_hDevNames != NULL)
{
LPDEVNAMES lpDevNames = (LPDEVNAMES)::GlobalLock(m_hDevNames);
ASSERT(lpDevNames != NULL);
does anyone have any ideas how to correct this? I have tried unlocking and creating a new instance e.g. PRINTDLG *pd = new PRINTDLG; to no avail.
----------------
Code:
void CTestprintdlg_memoryDlg::OnButton1()
{
PRINTDLG pd;
pd.lStructSize = sizeof( pd );
// Get MFC's printer
if(AfxGetApp()->GetPrinterDeviceDefaults(&pd) )
{
if (pd.hDevMode != NULL)
GlobalFree(pd.hDevMode);
if (pd.hDevNames != NULL)
GlobalFree(pd.hDevNames);
AfxMessageBox("ok");
}
}
- I had to format your code snippets because otherwise they are absolutely unreadable!
BTW, being here since more than a decade and having posted more than 80 posts you still ignore using code tags!? - How did you observe that "more and more memory is used without been free'd"?
- Why do you call GlobalFree if you did not allocate memory either with GlobalAlloc or GlobalReAlloc? Where in MSDN did you read that you should free memory after calling CWinApp::GetPrinterDeviceDefaults?
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|