From: Joseph M. Newcomer on
I don't know what the behavior is if you select a NULL handle as the current font; I
believe it is undefined. So it may be that the old font was not deselected.
joe

On Wed, 10 Mar 2010 21:27:34 -0600, "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote:

>Apparently this is not quite right:
>
> CFont* oldFont;
> CFont EmptyFont;
> oldFont = MemoryDC.SelectObject(&EmptyFont);
> oldFont->DeleteObject();
>
>I want to do this before this instruction:
> cfont.CreateFontIndirect(&LogFont);
>cfont is an object member variable that may be re-used
>over-and-over.
>
>"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in
>message news:d9jgp55513t0kb34pl3asj4qtiumk5frv0(a)4ax.com...
>> As I indicated in an earlier reply, you cannot call
>> DeleteObject on any object which is
>> selected into a DC. It won't do the deletion. It is you
>> responsibility to make sure the
>> objec tis not seelected. I suggest SaveDC/RestoreDC to
>> make sure any object selected are
>> released.
>> joe
>>
>>
>> On Wed, 10 Mar 2010 17:49:56 -0600, "Peter Olcott"
>> <NoSpam(a)OCR4Screen.com> wrote:
>>
>>>
>>>"Tom Serface" <tom(a)camaswood.com> wrote in message
>>>news:OwfKaEKwKHA.812(a)TK2MSFTNGP06.phx.gbl...
>>>> Yeah, that's it... Lots of time people forget to call
>>>> that. I turn on the GDI Objects column in Task Manager
>>>> so
>>>> I can watch what my program is doing.
>>>
>>>Can I execute CBitmap::DeleteObject() while the CBitmap is
>>>currently selected into a DC?
>>>(This is just before I create another CBitmap to be
>>>selected
>>>into the same DC).
>>>
>>>>
>>>> Tom
>>>>
>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>> news:H8mdnSY2XczicgrWnZ2dnUVZ_hWdnZ2d(a)giganews.com...
>>>>>
>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>>> news:WeWdnco19NaPcwrWnZ2dnUVZ_g2dnZ2d(a)giganews.com...
>>>>>>
>>>>>> "Tom Serface" <tom(a)camaswood.com> wrote in message
>>>>>> news:%23B3xq%23HwKHA.732(a)TK2MSFTNGP06.phx.gbl...
>>>>>>> You will have to manage the resources somehow and
>>>>>>> someplace, but I don't think it matters how or where.
>>>>>>> I think you get like 5000 GDI resources before they
>>>>>>> run
>>>>>>> out and I think that number is shared among programs.
>>>>>>> Still converting them is no worse that destroying and
>>>>>>> recreating them, but you'll have to destroy them
>>>>>>> ultimately at some point, or, if the number is not to
>>>>>>> arduous you could let them get recovered when the
>>>>>>> program exits (but I'm not a big fan of that
>>>>>>> behavior).
>>>>>>>
>>>>>>> Tom
>>>>>>
>>>>>> The main thing that I want to do is to manually free
>>>>>> any
>>>>>> memory or resources that the CBitmap object is using.
>>>>>> I
>>>>>> don't know what resources it is using, and I don't
>>>>>> know
>>>>>> how to free them.
>>>>>>
>>>>>
>>>>> I think that I got it: Simply call the inherited member
>>>>> DeleteObject().
>>>>>
>>>>>>>
>>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in
>>>>>>> message
>>>>>>> news:5oWdnfO3M6HKQArWnZ2dnUVZ_jCdnZ2d(a)giganews.com...
>>>>>>>> I am have built a general purpose bitmap/image
>>>>>>>> handling class and want to add TextOut() capability
>>>>>>>> to
>>>>>>>> this class. To do this I must convert some of my GDI
>>>>>>>> local function variables into GDI object member
>>>>>>>> variables. This means that I must be able to re-use
>>>>>>>> several GDI objects, instead of constructing them
>>>>>>>> and
>>>>>>>> destroying them after a single use.
>>>>>>>>
>>>>>>>> What issues are raised with resource and memory
>>>>>>>> leaks
>>>>>>>> by using CDC and CBitmap objects? How are these
>>>>>>>> issues mitigated?
>>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on
See below...
On Thu, 11 Mar 2010 10:14:12 -0600, "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote:

>
>"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in
>message news:d9jgp55513t0kb34pl3asj4qtiumk5frv0(a)4ax.com...
>> As I indicated in an earlier reply, you cannot call
>> DeleteObject on any object which is
>> selected into a DC. It won't do the deletion.
>
>Apparently this is no longer true under Windows 7. I spent
>several hours re-arranging my code to meet your
>specification, and ended up having to change it back. When I
>tested it against your prior post:
>
> If you have an object like CFont,
> you cannot create more than one
> font in it unles you call DeleteObject
> explivitly, or you will get an assertion failure.
****
This is not a Windows feature, but an MFC feature, and is usually caught by an assertion
in the ocde that creates a font, code of the form
ASSERT(something == NULL)
and if there is a handle associated with the font, the 'something' (I forget the variable
name) will com up non-NULL. This has alsays been a feature of MFC and it is there to
prevent errors cause by trying to create a font in a variable that already has a font. I'm
surprised that you did not encounter problems with it if you code assumed this was
possible. This is true for all the GDI objects (fonts, palettes, bitmaps, etc.)

You did not say which version of VS you are using, but it was true though VS 2008.

PTPH, the failure ot DeleteObject to delete an object selected into a DC has always been
trhough of as a bug, and it wouldn't surprise me if they fixed it but failed to tell us.
We are never told of bug ffixes.
*****
>
>and got an assertion failure when I removed the
>DeleteObject() for a CFont object that was currently
>selected into a MemoryDC. This apparently shows that
>DeleteObject() must work (at least for CFont) even if the
>CFont is currently selected into a MemoryDC.
******
CGDIObject::DeleteObject calls the kernel ::DeleteObject, fails to test the result, and
simply sends the m_hGdiObject field to NULL. This means that CGDIObject::DeleteObject
will meet the MFC requirements but does not guarantee that the objec is *actually*
deleteed. Under versions < Win7, and possibly in Win7 (since you have no evidence that
they fixed this "bug") the object remains in the kernel, consuming GDI resources, but MFC
works under the delusional system that the object has been deleted.
*******
>
>I had great difficulty trying to unselect the CFont and
>CBitmap objects. (see my prior post) The only way that I
>knew to unselect a GDI object was to select another GDI
>object. Since I had no other object, I tried to select an
>empty one. This did not work.
****
There was a reason I mentioned CDC::RestoreDC(). this will deselect all elements back to
the last SaveDC() and you don't need to have an "empty" object to select.
****
>
>> It is you responsibility to make sure the
>> objec tis not seelected. I suggest SaveDC/RestoreDC to
>> make sure any object selected are
>> released.
>> joe
>
>
>
>>
>>
>> On Wed, 10 Mar 2010 17:49:56 -0600, "Peter Olcott"
>> <NoSpam(a)OCR4Screen.com> wrote:
>>
>>>
>>>"Tom Serface" <tom(a)camaswood.com> wrote in message
>>>news:OwfKaEKwKHA.812(a)TK2MSFTNGP06.phx.gbl...
>>>> Yeah, that's it... Lots of time people forget to call
>>>> that. I turn on the GDI Objects column in Task Manager
>>>> so
>>>> I can watch what my program is doing.
>>>
>>>Can I execute CBitmap::DeleteObject() while the CBitmap is
>>>currently selected into a DC?
>>>(This is just before I create another CBitmap to be
>>>selected
>>>into the same DC).
>>>
>>>>
>>>> Tom
>>>>
>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>> news:H8mdnSY2XczicgrWnZ2dnUVZ_hWdnZ2d(a)giganews.com...
>>>>>
>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>>> news:WeWdnco19NaPcwrWnZ2dnUVZ_g2dnZ2d(a)giganews.com...
>>>>>>
>>>>>> "Tom Serface" <tom(a)camaswood.com> wrote in message
>>>>>> news:%23B3xq%23HwKHA.732(a)TK2MSFTNGP06.phx.gbl...
>>>>>>> You will have to manage the resources somehow and
>>>>>>> someplace, but I don't think it matters how or where.
>>>>>>> I think you get like 5000 GDI resources before they
>>>>>>> run
>>>>>>> out and I think that number is shared among programs.
>>>>>>> Still converting them is no worse that destroying and
>>>>>>> recreating them, but you'll have to destroy them
>>>>>>> ultimately at some point, or, if the number is not to
>>>>>>> arduous you could let them get recovered when the
>>>>>>> program exits (but I'm not a big fan of that
>>>>>>> behavior).
>>>>>>>
>>>>>>> Tom
>>>>>>
>>>>>> The main thing that I want to do is to manually free
>>>>>> any
>>>>>> memory or resources that the CBitmap object is using.
>>>>>> I
>>>>>> don't know what resources it is using, and I don't
>>>>>> know
>>>>>> how to free them.
>>>>>>
>>>>>
>>>>> I think that I got it: Simply call the inherited member
>>>>> DeleteObject().
>>>>>
>>>>>>>
>>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in
>>>>>>> message
>>>>>>> news:5oWdnfO3M6HKQArWnZ2dnUVZ_jCdnZ2d(a)giganews.com...
>>>>>>>> I am have built a general purpose bitmap/image
>>>>>>>> handling class and want to add TextOut() capability
>>>>>>>> to
>>>>>>>> this class. To do this I must convert some of my GDI
>>>>>>>> local function variables into GDI object member
>>>>>>>> variables. This means that I must be able to re-use
>>>>>>>> several GDI objects, instead of constructing them
>>>>>>>> and
>>>>>>>> destroying them after a single use.
>>>>>>>>
>>>>>>>> What issues are raised with resource and memory
>>>>>>>> leaks
>>>>>>>> by using CDC and CBitmap objects? How are these
>>>>>>>> issues mitigated?
>>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Tom Serface on
Hi Peter,

You can have different font objects if you want to have multiple fonts set
up. You are right about not deleting it when it is selected, but I don't
think you'd want that to happen. It typically make my font objects members
of the class so they are always persistent while the class object is...
Also, the assertion messages won't display for release builds which is
convenient, but if you get them in debug you would definitely want to fix
the problem.

Tom

"Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
news:hI6dnaNzGM1LiwTWnZ2dnUVZ_qSdnZ2d(a)giganews.com...
>
> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
> news:d9jgp55513t0kb34pl3asj4qtiumk5frv0(a)4ax.com...
>> As I indicated in an earlier reply, you cannot call DeleteObject on any
>> object which is
>> selected into a DC. It won't do the deletion.
>
> Apparently this is no longer true under Windows 7. I spent several hours
> re-arranging my code to meet your specification, and ended up having to
> change it back. When I tested it against your prior post:
>
> If you have an object like CFont,
> you cannot create more than one
> font in it unles you call DeleteObject
> explivitly, or you will get an assertion failure.
>
> and got an assertion failure when I removed the DeleteObject() for a CFont
> object that was currently selected into a MemoryDC. This apparently shows
> that DeleteObject() must work (at least for CFont) even if the CFont is
> currently selected into a MemoryDC.
>
> I had great difficulty trying to unselect the CFont and CBitmap objects.
> (see my prior post) The only way that I knew to unselect a GDI object was
> to select another GDI object. Since I had no other object, I tried to
> select an empty one. This did not work.
>
>> It is you responsibility to make sure the
>> objec tis not seelected. I suggest SaveDC/RestoreDC to make sure any
>> object selected are
>> released.
>> joe
>
>
>
>>
>>
>> On Wed, 10 Mar 2010 17:49:56 -0600, "Peter Olcott"
>> <NoSpam(a)OCR4Screen.com> wrote:
>>
>>>
>>>"Tom Serface" <tom(a)camaswood.com> wrote in message
>>>news:OwfKaEKwKHA.812(a)TK2MSFTNGP06.phx.gbl...
>>>> Yeah, that's it... Lots of time people forget to call
>>>> that. I turn on the GDI Objects column in Task Manager so
>>>> I can watch what my program is doing.
>>>
>>>Can I execute CBitmap::DeleteObject() while the CBitmap is
>>>currently selected into a DC?
>>>(This is just before I create another CBitmap to be selected
>>>into the same DC).
>>>
>>>>
>>>> Tom
>>>>
>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>> news:H8mdnSY2XczicgrWnZ2dnUVZ_hWdnZ2d(a)giganews.com...
>>>>>
>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>>> news:WeWdnco19NaPcwrWnZ2dnUVZ_g2dnZ2d(a)giganews.com...
>>>>>>
>>>>>> "Tom Serface" <tom(a)camaswood.com> wrote in message
>>>>>> news:%23B3xq%23HwKHA.732(a)TK2MSFTNGP06.phx.gbl...
>>>>>>> You will have to manage the resources somehow and
>>>>>>> someplace, but I don't think it matters how or where.
>>>>>>> I think you get like 5000 GDI resources before they run
>>>>>>> out and I think that number is shared among programs.
>>>>>>> Still converting them is no worse that destroying and
>>>>>>> recreating them, but you'll have to destroy them
>>>>>>> ultimately at some point, or, if the number is not to
>>>>>>> arduous you could let them get recovered when the
>>>>>>> program exits (but I'm not a big fan of that behavior).
>>>>>>>
>>>>>>> Tom
>>>>>>
>>>>>> The main thing that I want to do is to manually free any
>>>>>> memory or resources that the CBitmap object is using. I
>>>>>> don't know what resources it is using, and I don't know
>>>>>> how to free them.
>>>>>>
>>>>>
>>>>> I think that I got it: Simply call the inherited member
>>>>> DeleteObject().
>>>>>
>>>>>>>
>>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>>>>> news:5oWdnfO3M6HKQArWnZ2dnUVZ_jCdnZ2d(a)giganews.com...
>>>>>>>> I am have built a general purpose bitmap/image
>>>>>>>> handling class and want to add TextOut() capability to
>>>>>>>> this class. To do this I must convert some of my GDI
>>>>>>>> local function variables into GDI object member
>>>>>>>> variables. This means that I must be able to re-use
>>>>>>>> several GDI objects, instead of constructing them and
>>>>>>>> destroying them after a single use.
>>>>>>>>
>>>>>>>> What issues are raised with resource and memory leaks
>>>>>>>> by using CDC and CBitmap objects? How are these
>>>>>>>> issues mitigated?
>>>>>>>>
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>
>> Joseph M. Newcomer [MVP]
>> email: newcomer(a)flounder.com
>> Web: http://www.flounder.com
>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
>
From: Joseph M. Newcomer on
Tom,

you would think that in any sane system, such as one that uses kernel object reference
counts [like Windows], that "deleting" a font that was actively selected into a DC would
reduce the reference count to 1, so that when the font was deselected, the reference count
would go to 0 and at that point the now-unused font would be freed and the SelectObject
would rreturn a NULL handle. But that would probably be sensible, so instead, the font is
left live and you continue execution with the impression that the HFONT is now referring
to a nonexistent font, so you just ovewrite the handle and thus lose any access to the
font, which continues to exist. (for HFONT, you can substitute HPEN, HBRUSH, HPALETTE,
HBITMAP, etc.).

Apparently, because I forgot to put the :: in front of ::DeleteObject, I left it ambiguous
as to what I was talking about. CGdiObject;:DeleteObject produces the illusion that it
"worked", because it allows the CFont to be reused, even though the actual font object in
the kernel has not been deleted and is still occupying GDI kernel space. My bad. The
truth is that ::DeleteObject will not delete (a) any GDI object selected into a DC (b) any
stock object. While we don't expect stock objects to be deleted, the fact that objects
can be left "dangling" always comes as a surprise, and it is not helped by the fact that
CGdiObject::DeleteObject preserves the correct MFC state so that it *appears* that it
worked succesfully (even though it didn't actually delete the object).
joe

On Fri, 12 Mar 2010 12:25:12 -0800, "Tom Serface" <tom(a)camaswood.com> wrote:

>Hi Peter,
>
>You can have different font objects if you want to have multiple fonts set
>up. You are right about not deleting it when it is selected, but I don't
>think you'd want that to happen. It typically make my font objects members
>of the class so they are always persistent while the class object is...
>Also, the assertion messages won't display for release builds which is
>convenient, but if you get them in debug you would definitely want to fix
>the problem.
>
>Tom
>
>"Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>news:hI6dnaNzGM1LiwTWnZ2dnUVZ_qSdnZ2d(a)giganews.com...
>>
>> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in message
>> news:d9jgp55513t0kb34pl3asj4qtiumk5frv0(a)4ax.com...
>>> As I indicated in an earlier reply, you cannot call DeleteObject on any
>>> object which is
>>> selected into a DC. It won't do the deletion.
>>
>> Apparently this is no longer true under Windows 7. I spent several hours
>> re-arranging my code to meet your specification, and ended up having to
>> change it back. When I tested it against your prior post:
>>
>> If you have an object like CFont,
>> you cannot create more than one
>> font in it unles you call DeleteObject
>> explivitly, or you will get an assertion failure.
>>
>> and got an assertion failure when I removed the DeleteObject() for a CFont
>> object that was currently selected into a MemoryDC. This apparently shows
>> that DeleteObject() must work (at least for CFont) even if the CFont is
>> currently selected into a MemoryDC.
>>
>> I had great difficulty trying to unselect the CFont and CBitmap objects.
>> (see my prior post) The only way that I knew to unselect a GDI object was
>> to select another GDI object. Since I had no other object, I tried to
>> select an empty one. This did not work.
>>
>>> It is you responsibility to make sure the
>>> objec tis not seelected. I suggest SaveDC/RestoreDC to make sure any
>>> object selected are
>>> released.
>>> joe
>>
>>
>>
>>>
>>>
>>> On Wed, 10 Mar 2010 17:49:56 -0600, "Peter Olcott"
>>> <NoSpam(a)OCR4Screen.com> wrote:
>>>
>>>>
>>>>"Tom Serface" <tom(a)camaswood.com> wrote in message
>>>>news:OwfKaEKwKHA.812(a)TK2MSFTNGP06.phx.gbl...
>>>>> Yeah, that's it... Lots of time people forget to call
>>>>> that. I turn on the GDI Objects column in Task Manager so
>>>>> I can watch what my program is doing.
>>>>
>>>>Can I execute CBitmap::DeleteObject() while the CBitmap is
>>>>currently selected into a DC?
>>>>(This is just before I create another CBitmap to be selected
>>>>into the same DC).
>>>>
>>>>>
>>>>> Tom
>>>>>
>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>>> news:H8mdnSY2XczicgrWnZ2dnUVZ_hWdnZ2d(a)giganews.com...
>>>>>>
>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>>>> news:WeWdnco19NaPcwrWnZ2dnUVZ_g2dnZ2d(a)giganews.com...
>>>>>>>
>>>>>>> "Tom Serface" <tom(a)camaswood.com> wrote in message
>>>>>>> news:%23B3xq%23HwKHA.732(a)TK2MSFTNGP06.phx.gbl...
>>>>>>>> You will have to manage the resources somehow and
>>>>>>>> someplace, but I don't think it matters how or where.
>>>>>>>> I think you get like 5000 GDI resources before they run
>>>>>>>> out and I think that number is shared among programs.
>>>>>>>> Still converting them is no worse that destroying and
>>>>>>>> recreating them, but you'll have to destroy them
>>>>>>>> ultimately at some point, or, if the number is not to
>>>>>>>> arduous you could let them get recovered when the
>>>>>>>> program exits (but I'm not a big fan of that behavior).
>>>>>>>>
>>>>>>>> Tom
>>>>>>>
>>>>>>> The main thing that I want to do is to manually free any
>>>>>>> memory or resources that the CBitmap object is using. I
>>>>>>> don't know what resources it is using, and I don't know
>>>>>>> how to free them.
>>>>>>>
>>>>>>
>>>>>> I think that I got it: Simply call the inherited member
>>>>>> DeleteObject().
>>>>>>
>>>>>>>>
>>>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>>>>>>> news:5oWdnfO3M6HKQArWnZ2dnUVZ_jCdnZ2d(a)giganews.com...
>>>>>>>>> I am have built a general purpose bitmap/image
>>>>>>>>> handling class and want to add TextOut() capability to
>>>>>>>>> this class. To do this I must convert some of my GDI
>>>>>>>>> local function variables into GDI object member
>>>>>>>>> variables. This means that I must be able to re-use
>>>>>>>>> several GDI objects, instead of constructing them and
>>>>>>>>> destroying them after a single use.
>>>>>>>>>
>>>>>>>>> What issues are raised with resource and memory leaks
>>>>>>>>> by using CDC and CBitmap objects? How are these
>>>>>>>>> issues mitigated?
>>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>>
>>>>>>
>>>>
>>> Joseph M. Newcomer [MVP]
>>> email: newcomer(a)flounder.com
>>> Web: http://www.flounder.com
>>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>>
>>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Peter Olcott on

"Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in
message news:ailop51jv036cnv0dq6po6eteuo3g7dfbv(a)4ax.com...
> Tom,
>
> you would think that in any sane system, such as one that
> uses kernel object reference
> counts [like Windows], that "deleting" a font that was
> actively selected into a DC would
> reduce the reference count to 1, so that when the font was
> deselected, the reference count
> would go to 0 and at that point the now-unused font would
> be freed and the SelectObject
> would rreturn a NULL handle. But that would probably be
> sensible, so instead, the font is
> left live and you continue execution with the impression
> that the HFONT is now referring
> to a nonexistent font, so you just ovewrite the handle and
> thus lose any access to the
> font, which continues to exist. (for HFONT, you can
> substitute HPEN, HBRUSH, HPALETTE,
> HBITMAP, etc.).
>
> Apparently, because I forgot to put the :: in front of
> ::DeleteObject, I left it ambiguous
> as to what I was talking about. CGdiObject;:DeleteObject
> produces the illusion that it
> "worked", because it allows the CFont to be reused, even
> though the actual font object in
> the kernel has not been deleted and is still occupying GDI
> kernel space. My bad. The
> truth is that ::DeleteObject will not delete (a) any GDI
> object selected into a DC (b) any
> stock object. While we don't expect stock objects to be
> deleted, the fact that objects
> can be left "dangling" always comes as a surprise, and it
> is not helped by the fact that
> CGdiObject::DeleteObject preserves the correct MFC state
> so that it *appears* that it
> worked succesfully (even though it didn't actually delete
> the object).
> joe

So what is an example of simple clean minimal syntax for
making sure that a single set of GDI object member variables
always does get properly deleted? The best that I could
come up with is to duplicate everything such as CFont
cfont[2]; and toggle the subscript.
This seems like far too much of a kludge.

>
> On Fri, 12 Mar 2010 12:25:12 -0800, "Tom Serface"
> <tom(a)camaswood.com> wrote:
>
>>Hi Peter,
>>
>>You can have different font objects if you want to have
>>multiple fonts set
>>up. You are right about not deleting it when it is
>>selected, but I don't
>>think you'd want that to happen. It typically make my
>>font objects members
>>of the class so they are always persistent while the class
>>object is...
>>Also, the assertion messages won't display for release
>>builds which is
>>convenient, but if you get them in debug you would
>>definitely want to fix
>>the problem.
>>
>>Tom
>>
>>"Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in message
>>news:hI6dnaNzGM1LiwTWnZ2dnUVZ_qSdnZ2d(a)giganews.com...
>>>
>>> "Joseph M. Newcomer" <newcomer(a)flounder.com> wrote in
>>> message
>>> news:d9jgp55513t0kb34pl3asj4qtiumk5frv0(a)4ax.com...
>>>> As I indicated in an earlier reply, you cannot call
>>>> DeleteObject on any
>>>> object which is
>>>> selected into a DC. It won't do the deletion.
>>>
>>> Apparently this is no longer true under Windows 7. I
>>> spent several hours
>>> re-arranging my code to meet your specification, and
>>> ended up having to
>>> change it back. When I tested it against your prior
>>> post:
>>>
>>> If you have an object like CFont,
>>> you cannot create more than one
>>> font in it unles you call DeleteObject
>>> explivitly, or you will get an assertion failure.
>>>
>>> and got an assertion failure when I removed the
>>> DeleteObject() for a CFont
>>> object that was currently selected into a MemoryDC.
>>> This apparently shows
>>> that DeleteObject() must work (at least for CFont) even
>>> if the CFont is
>>> currently selected into a MemoryDC.
>>>
>>> I had great difficulty trying to unselect the CFont and
>>> CBitmap objects.
>>> (see my prior post) The only way that I knew to unselect
>>> a GDI object was
>>> to select another GDI object. Since I had no other
>>> object, I tried to
>>> select an empty one. This did not work.
>>>
>>>> It is you responsibility to make sure the
>>>> objec tis not seelected. I suggest SaveDC/RestoreDC to
>>>> make sure any
>>>> object selected are
>>>> released.
>>>> joe
>>>
>>>
>>>
>>>>
>>>>
>>>> On Wed, 10 Mar 2010 17:49:56 -0600, "Peter Olcott"
>>>> <NoSpam(a)OCR4Screen.com> wrote:
>>>>
>>>>>
>>>>>"Tom Serface" <tom(a)camaswood.com> wrote in message
>>>>>news:OwfKaEKwKHA.812(a)TK2MSFTNGP06.phx.gbl...
>>>>>> Yeah, that's it... Lots of time people forget to
>>>>>> call
>>>>>> that. I turn on the GDI Objects column in Task
>>>>>> Manager so
>>>>>> I can watch what my program is doing.
>>>>>
>>>>>Can I execute CBitmap::DeleteObject() while the CBitmap
>>>>>is
>>>>>currently selected into a DC?
>>>>>(This is just before I create another CBitmap to be
>>>>>selected
>>>>>into the same DC).
>>>>>
>>>>>>
>>>>>> Tom
>>>>>>
>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in
>>>>>> message
>>>>>> news:H8mdnSY2XczicgrWnZ2dnUVZ_hWdnZ2d(a)giganews.com...
>>>>>>>
>>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in
>>>>>>> message
>>>>>>> news:WeWdnco19NaPcwrWnZ2dnUVZ_g2dnZ2d(a)giganews.com...
>>>>>>>>
>>>>>>>> "Tom Serface" <tom(a)camaswood.com> wrote in message
>>>>>>>> news:%23B3xq%23HwKHA.732(a)TK2MSFTNGP06.phx.gbl...
>>>>>>>>> You will have to manage the resources somehow and
>>>>>>>>> someplace, but I don't think it matters how or
>>>>>>>>> where.
>>>>>>>>> I think you get like 5000 GDI resources before
>>>>>>>>> they run
>>>>>>>>> out and I think that number is shared among
>>>>>>>>> programs.
>>>>>>>>> Still converting them is no worse that destroying
>>>>>>>>> and
>>>>>>>>> recreating them, but you'll have to destroy them
>>>>>>>>> ultimately at some point, or, if the number is not
>>>>>>>>> to
>>>>>>>>> arduous you could let them get recovered when the
>>>>>>>>> program exits (but I'm not a big fan of that
>>>>>>>>> behavior).
>>>>>>>>>
>>>>>>>>> Tom
>>>>>>>>
>>>>>>>> The main thing that I want to do is to manually
>>>>>>>> free any
>>>>>>>> memory or resources that the CBitmap object is
>>>>>>>> using. I
>>>>>>>> don't know what resources it is using, and I don't
>>>>>>>> know
>>>>>>>> how to free them.
>>>>>>>>
>>>>>>>
>>>>>>> I think that I got it: Simply call the inherited
>>>>>>> member
>>>>>>> DeleteObject().
>>>>>>>
>>>>>>>>>
>>>>>>>>> "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote in
>>>>>>>>> message
>>>>>>>>> news:5oWdnfO3M6HKQArWnZ2dnUVZ_jCdnZ2d(a)giganews.com...
>>>>>>>>>> I am have built a general purpose bitmap/image
>>>>>>>>>> handling class and want to add TextOut()
>>>>>>>>>> capability to
>>>>>>>>>> this class. To do this I must convert some of my
>>>>>>>>>> GDI
>>>>>>>>>> local function variables into GDI object member
>>>>>>>>>> variables. This means that I must be able to
>>>>>>>>>> re-use
>>>>>>>>>> several GDI objects, instead of constructing them
>>>>>>>>>> and
>>>>>>>>>> destroying them after a single use.
>>>>>>>>>>
>>>>>>>>>> What issues are raised with resource and memory
>>>>>>>>>> leaks
>>>>>>>>>> by using CDC and CBitmap objects? How are these
>>>>>>>>>> issues mitigated?
>>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>>
>>>>>
>>>> Joseph M. Newcomer [MVP]
>>>> email: newcomer(a)flounder.com
>>>> Web: http://www.flounder.com
>>>> MVP Tips: http://www.flounder.com/mvp_tips.htm
>>>
>>>
> Joseph M. Newcomer [MVP]
> email: newcomer(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm