Microsoft KB Archive/117686

= DOC: CDC::SelectObject, CDC::SelectStockObject Return =

Article ID: 117686

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft C/C++ Professional Development System 7.0

 Microsoft Visual C++ 1.0 Professional Edition

 Microsoft Visual C++ 1.5 Professional Edition

 Microsoft Visual C++ 1.51</li></ul>

 Microsoft Visual C++ 1.52 Professional Edition</li></ul>

 Microsoft Visual C++ 1.0 Professional Edition</li></ul>

 Microsoft Visual C++ 2.0 Professional Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q117686

<div class="summary_section">

SUMMARY
The pointer to a CGdiObject that is returned from CDC::SelectObject or CDC::SelectStockObject is stored for use. When the pointer is used later, it causes unexpected behavior [for example, a general protection fault (GPF) or heap corruption].

The documentation for the SelectObject member does not make clear that the pointer returned from the call might point to a temporary object that is only valid during processing of one Windows message. This is an error in the documentation.

The CDC::SelectObject and CDC::SelectStockObject functions make a call to CGdiObject::FromHandle to determine which CGdiObject object to return. If there is no CGdiObject object attached to the Windows GDI object that is currently selected in the DC, then a temporary object is created. See the documentation for CGdiObject::FromHandle for further information.

<div class="moreinformation_section">

MORE INFORMATION
If you want to store the object that was previously selected in the DC, then you should store the Windows handle to the GDI object (HGDIOBJ), not a pointer to the CGdiObject. The handle can be obtained from the temporary file by using the function CGdiObject::GetSafeHandle. You can later use the function CGdiObject::FromHandle to obtain a pointer to a CGdiObject that encapsulates the HGDIOBJ. For example: ...  // Select a pen in the DC. CPen *ppenOld = pDC->SelectObject(ppenNew); m_hpenOld = (HPEN) ppenOld->GetSafeHandle; ...  ...   // In some other message handler we wish to   // restore the old pen. CPen *ppenOld = CPen::FromHandle(m_hpenOld); pDC->SelectObject(ppenOld); ...

<div class="references_section">