Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Problem in using Fromhandle Function

Problem in using Fromhandle Function

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    ashtwin
    wrote on last edited by
    #1

    Hi, i am saving a HDC in one fuction of a class by using the following code MyClass::MyFunction() { CDC dc; AfxGetApp()->CreatePrinterDC(dc); m_Hdc = dc.GetSafeHdc( ); dc.Detach(); } Now in some other classes i am Creating a CDC class objcet using this HDC. CDC *pDC = CDC::FromHandle(MyClasss::getMyHDC()); But some time it is causing my application to crash. But if i replace FromHandle() by Attach() and detach() the application is working fine. So, i just want to know whether it is safe to use FromHandle() or not. If not should i replace all my FromHandle() by Attach and detach. Thanks Ashwani

    R 1 Reply Last reply
    0
    • A ashtwin

      Hi, i am saving a HDC in one fuction of a class by using the following code MyClass::MyFunction() { CDC dc; AfxGetApp()->CreatePrinterDC(dc); m_Hdc = dc.GetSafeHdc( ); dc.Detach(); } Now in some other classes i am Creating a CDC class objcet using this HDC. CDC *pDC = CDC::FromHandle(MyClasss::getMyHDC()); But some time it is causing my application to crash. But if i replace FromHandle() by Attach() and detach() the application is working fine. So, i just want to know whether it is safe to use FromHandle() or not. If not should i replace all my FromHandle() by Attach and detach. Thanks Ashwani

      R Offline
      R Offline
      Rajkumar R
      wrote on last edited by
      #2

      ashtwin wrote:

      CDC *pDC = CDC::FromHandle(MyClasss::getMyHDC());

      Are you storing the pDC beyond a function call, CDC::FromHandle returns a temporary CDC object if HDC is not already attached. CDC::DeleteTempMap will delete the temporary CDC objects in idle processing. so if you using the object beyond a function call and there by alowing the idle processing to delete the object, application can crash if you access the temp object. CDC::FromHandle() is less error-prone in the sense you do not have to remember to "detach". but remember it is used only in immediate processing. In your MyClass::MyFunction(), you are Detaching the CDC, so it is sure that FromHandle creates temp map object, and possibly you are storing beyond a function call. I suggest you to Keep attached the Printer DC with a member variable CDC and use it everywhere or use attach detach you should match the two calls then, or use FromHandle but with in a function call. If the frequency of the call is high reduce the number of attach - detach.

      modified on Friday, March 14, 2008 8:50 AM

      A 1 Reply Last reply
      0
      • R Rajkumar R

        ashtwin wrote:

        CDC *pDC = CDC::FromHandle(MyClasss::getMyHDC());

        Are you storing the pDC beyond a function call, CDC::FromHandle returns a temporary CDC object if HDC is not already attached. CDC::DeleteTempMap will delete the temporary CDC objects in idle processing. so if you using the object beyond a function call and there by alowing the idle processing to delete the object, application can crash if you access the temp object. CDC::FromHandle() is less error-prone in the sense you do not have to remember to "detach". but remember it is used only in immediate processing. In your MyClass::MyFunction(), you are Detaching the CDC, so it is sure that FromHandle creates temp map object, and possibly you are storing beyond a function call. I suggest you to Keep attached the Printer DC with a member variable CDC and use it everywhere or use attach detach you should match the two calls then, or use FromHandle but with in a function call. If the frequency of the call is high reduce the number of attach - detach.

        modified on Friday, March 14, 2008 8:50 AM

        A Offline
        A Offline
        ashtwin
        wrote on last edited by
        #3

        Hi, Currently i am using the pDC within the same fuction call though i am calling a seperate function to create it which is returning this pDC to the parent function and again from the parent function i am calling a seperate function which has pDC as one of its parameter. CDC* dc = myPrintObj.createDCForPrint(printRect); doPrintWfReport(dc, printJob); The Funtion where i am getting the CDC object is as follows CDC* PrintJob::PrintJobToPrinterType::createDCForPrint(const NcWfPrintJob& client, const CRect& printRect) { REQUIRE(!printRect.IsRectEmpty()); DOCINFO DI; ZeroMemory(&DI, sizeof(DOCINFO)); DI.cbSize = sizeof(DOCINFO); DI.lpszDocName = NcCommonResources().getWFReportDocName(); CDC* pDC = CDC::FromHandle(client.getPrintInfo().hDC); pDC->SetMapMode(MM_HIMETRIC); pDC->StartDoc(&DI); pDC->StartPage(); return pDC; } In the current design it is not possible for me to have a CDC object as a member varaible. Is it OK to use FromHandlePermanent() instead of FromHandle(). Thanks

        R 1 Reply Last reply
        0
        • A ashtwin

          Hi, Currently i am using the pDC within the same fuction call though i am calling a seperate function to create it which is returning this pDC to the parent function and again from the parent function i am calling a seperate function which has pDC as one of its parameter. CDC* dc = myPrintObj.createDCForPrint(printRect); doPrintWfReport(dc, printJob); The Funtion where i am getting the CDC object is as follows CDC* PrintJob::PrintJobToPrinterType::createDCForPrint(const NcWfPrintJob& client, const CRect& printRect) { REQUIRE(!printRect.IsRectEmpty()); DOCINFO DI; ZeroMemory(&DI, sizeof(DOCINFO)); DI.cbSize = sizeof(DOCINFO); DI.lpszDocName = NcCommonResources().getWFReportDocName(); CDC* pDC = CDC::FromHandle(client.getPrintInfo().hDC); pDC->SetMapMode(MM_HIMETRIC); pDC->StartDoc(&DI); pDC->StartPage(); return pDC; } In the current design it is not possible for me to have a CDC object as a member varaible. Is it OK to use FromHandlePermanent() instead of FromHandle(). Thanks

          R Offline
          R Offline
          Rajkumar R
          wrote on last edited by
          #4

          Do you have FromHandlePermanent for CDC, i think it is for CWnd::FromHandlePermanent.

          ashtwin wrote:

          Currently i am using the pDC within the same fuction call

          I assume this is your new change, and works without crash.

          ashtwin wrote:

          In the current design it is not possible for me to have a CDC object as a member varaible

          why it is not possible, in createDCForPrint why don't you attach the printer DC to a member variable CDC::Attach and when you done printing you can call cleanup function like DeleteDCForPrint and detach and delete the DC stored in member variable. If you are using the DC within the function call as said above it is ok, make sure the idle processing which deletes the temp DC object not allowed to execute in between.

          A 1 Reply Last reply
          0
          • R Rajkumar R

            Do you have FromHandlePermanent for CDC, i think it is for CWnd::FromHandlePermanent.

            ashtwin wrote:

            Currently i am using the pDC within the same fuction call

            I assume this is your new change, and works without crash.

            ashtwin wrote:

            In the current design it is not possible for me to have a CDC object as a member varaible

            why it is not possible, in createDCForPrint why don't you attach the printer DC to a member variable CDC::Attach and when you done printing you can call cleanup function like DeleteDCForPrint and detach and delete the DC stored in member variable. If you are using the DC within the function call as said above it is ok, make sure the idle processing which deletes the temp DC object not allowed to execute in between.

            A Offline
            A Offline
            ashtwin
            wrote on last edited by
            #5

            Sorry, u r right CDC don't have FromHandlePermanent(). Now the question is whether we can return a temporary object pointer from a function or not? Thanks Ashwani

            R 1 Reply Last reply
            0
            • A ashtwin

              Sorry, u r right CDC don't have FromHandlePermanent(). Now the question is whether we can return a temporary object pointer from a function or not? Thanks Ashwani

              R Offline
              R Offline
              Rajkumar R
              wrote on last edited by
              #6

              ashtwin wrote:

              Now the question is whether we can return a temporary object pointer from a function or not?

              That i answered repeatedly, if you are sure the idle processing is not allowed to execute that is used CDC within a function callstack which is running in the same thread as of the Message loop ad you are not doing windows message processing in between.

              A 1 Reply Last reply
              0
              • R Rajkumar R

                ashtwin wrote:

                Now the question is whether we can return a temporary object pointer from a function or not?

                That i answered repeatedly, if you are sure the idle processing is not allowed to execute that is used CDC within a function callstack which is running in the same thread as of the Message loop ad you are not doing windows message processing in between.

                A Offline
                A Offline
                ashtwin
                wrote on last edited by
                #7

                Ok, thanks. I think dc.StartDoc; is the culprit. Because after prompting Save as dialogue the system will become idle and temporary objects are getting deleted during this call only.

                R 1 Reply Last reply
                0
                • A ashtwin

                  Ok, thanks. I think dc.StartDoc; is the culprit. Because after prompting Save as dialogue the system will become idle and temporary objects are getting deleted during this call only.

                  R Offline
                  R Offline
                  Rajkumar R
                  wrote on last edited by
                  #8

                  Then what about returning an object which is attached using CDC::Attach not CDC::FromHandle.

                  CDC *CreatePrinterDC()
                  {
                  ....
                  CDC *pdcPrinter = new CDC;
                  pdcPrinter ->Attach(hdcPrinter);
                  ....
                  return pdcPrinter;
                  }

                  clean up

                  delete pdcPrinter;

                  in main function;

                  A 1 Reply Last reply
                  0
                  • R Rajkumar R

                    Then what about returning an object which is attached using CDC::Attach not CDC::FromHandle.

                    CDC *CreatePrinterDC()
                    {
                    ....
                    CDC *pdcPrinter = new CDC;
                    pdcPrinter ->Attach(hdcPrinter);
                    ....
                    return pdcPrinter;
                    }

                    clean up

                    delete pdcPrinter;

                    in main function;

                    A Offline
                    A Offline
                    ashtwin
                    wrote on last edited by
                    #9

                    Hi, i think the solution which u r suggesting will definately work. But i have overridden createDCForPrint() and in one function i am already using new to create CMetaFileDC object. I will try to implement ur solution if possible. Thanks

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups