Problem with SelectObject()
-
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.
-
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.
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++ -
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++ -
You can reuse the device-context how many times you want. The only thing you should be aware of is that:
- each time you select a GDI object (font, pen, brush, etc.) into a device-context you should restore the one that it held originally
- 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 theCWinApp::OnIdle
). This means that you should restore these objects before returning from the message handler on which you have calledSelectObject
-
You can reuse the device-context how many times you want. The only thing you should be aware of is that:
- each time you select a GDI object (font, pen, brush, etc.) into a device-context you should restore the one that it held originally
- 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 theCWinApp::OnIdle
). This means that you should restore these objects before returning from the message handler on which you have calledSelectObject
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.
-
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.
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.
-
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.
-
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.