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 with SelectObject()

Problem with SelectObject()

Scheduled Pinned Locked Moved C / C++ / MFC
graphicshelp
8 Posts 4 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 using SelectObject() in my code but confused with the MSDN description.

    CFont font;
    font.CreateFont(-12, 0, 0, 0, 400, FALSE, TRUE, FALSE, ANSI_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, _T("Courier New"));
    CFont* oldFont = pdc->SelectObject(font);
    pdc->TextOut(0, 0, str);
    pdc->SelectObject(oldFont);
    font.DeleteObject();

    As per MSDN we should not use the object return by SelectObject because it is a temporary one. But if we are restoring the old GDI object in the above way will it cause any problem because the same dc can be used in some other place.

    C 1 Reply Last reply
    0
    • A ashtwin

      Hi, I am using SelectObject() in my code but confused with the MSDN description.

      CFont font;
      font.CreateFont(-12, 0, 0, 0, 400, FALSE, TRUE, FALSE, ANSI_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, FIXED_PITCH | FF_MODERN, _T("Courier New"));
      CFont* oldFont = pdc->SelectObject(font);
      pdc->TextOut(0, 0, str);
      pdc->SelectObject(oldFont);
      font.DeleteObject();

      As per MSDN we should not use the object return by SelectObject because it is a temporary one. But if we are restoring the old GDI object in the above way will it cause any problem because the same dc can be used in some other place.

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      MSDN says: This function may return a pointer to a temporary object. This temporary object is only valid during the processing of one Windows message. So, it is ok to replace the selected object by the previous object like you did in your case.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      A 1 Reply Last reply
      0
      • C Cedric Moonen

        MSDN says: This function may return a pointer to a temporary object. This temporary object is only valid during the processing of one Windows message. So, it is ok to replace the selected object by the previous object like you did in your case.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

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

        Thanks for the reply. But if i want to use the same DC after this call wiil it be ok.

        S 1 Reply Last reply
        0
        • A ashtwin

          Thanks for the reply. But if i want to use the same DC after this call wiil it be ok.

          S Offline
          S Offline
          Sauro Viti
          wrote on last edited by
          #4

          You can reuse the device-context how many times you want. The only thing you should be aware of is that:

          1. each time you select a GDI object (font, pen, brush, etc.) into a device-context you should restore the one that it held originally
          2. you should not save the pointers returned by SelectObject to restore them later, because they could be temporary. To be more specific, the objects pointed could be destroyed during the idle-time of your application (i.e. inside the CWinApp::OnIdle). This means that you should restore these objects before returning from the message handler on which you have called SelectObject
          A 1 Reply Last reply
          0
          • S Sauro Viti

            You can reuse the device-context how many times you want. The only thing you should be aware of is that:

            1. each time you select a GDI object (font, pen, brush, etc.) into a device-context you should restore the one that it held originally
            2. you should not save the pointers returned by SelectObject to restore them later, because they could be temporary. To be more specific, the objects pointed could be destroyed during the idle-time of your application (i.e. inside the CWinApp::OnIdle). This means that you should restore these objects before returning from the message handler on which you have called SelectObject
            A Offline
            A Offline
            ashtwin
            wrote on last edited by
            #5

            But if we restore the temporary pointer returned from SelectObject in the DC immediately, the DC may be having some dangling references after the current call. Means if we store the current DC for some other call there are chances of using incorrect memory.

            S N 2 Replies Last reply
            0
            • A ashtwin

              But if we restore the temporary pointer returned from SelectObject in the DC immediately, the DC may be having some dangling references after the current call. Means if we store the current DC for some other call there are chances of using incorrect memory.

              S Offline
              S Offline
              Sauro Viti
              wrote on last edited by
              #6

              ashtwin wrote:

              But if we restore the temporary pointer returned from SelectObject in the DC immediately, the DC may be having some dangling references after the current call.

              I think you mean: "the device-context hold a pointer that could became invalid". Isn't it? Don't worry about it: once you have selected back the original object inside the device-context, everything will works fine. What you cannot do is to store that pointer and use it explicitly later.

              1 Reply Last reply
              0
              • A ashtwin

                But if we restore the temporary pointer returned from SelectObject in the DC immediately, the DC may be having some dangling references after the current call. Means if we store the current DC for some other call there are chances of using incorrect memory.

                N Offline
                N Offline
                Niklas L
                wrote on last edited by
                #7

                There is a difference. The temporary object is a temporary MFC object wrapping the underlying HGDIOBJECT. The gdi object the handle is pointing to is not temporary.

                home

                A 1 Reply Last reply
                0
                • N Niklas L

                  There is a difference. The temporary object is a temporary MFC object wrapping the underlying HGDIOBJECT. The gdi object the handle is pointing to is not temporary.

                  home

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

                  I understood. Thanks you all for your replies.

                  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