MFC PRINTDLG release memory issue
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: MFC PRINTDLG release memory issue

  1. #1
    Join Date
    Dec 1999
    Posts
    83

    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");

    }
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,301

    Re: MFC PRINTDLG release memory issue

    Quote Originally Posted by hobnob View Post
    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");
    	}	
    }
    1. 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!?
    2. How did you observe that "more and more memory is used without been free'd"?
    3. 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center