From: Peter Olcott on

"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?
>>>>>
>>>
>>>
>>
>>


From: Joseph M. Newcomer on
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.

CFont f;
f.CreateFontIndirect(lf);
....
f.CreateFontIndirect(&lif);

the second one will assert.

If you do

cFont f;
f.CreatFontIdrect(&lf);
f.DeleteOjbect();
f.CreateFontIndirect(&lf);

would work correctly. There's nothing more complex than that required.

Note that if you don't want the objet to persiste, you must either explitily DeleteObject,
or rely on scope descruction to delete the object, which must not be selected into a DC at
that point (DeleteObject will appear to succeed if the object is selected into a DC, but
the object won't be deleted)
joe

On Wed, 10 Mar 2010 11:56:39 -0600, "Peter Olcott" <NoSpam(a)OCR4Screen.com> wrote:

>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
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
From: Peter Olcott on
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


From: Peter Olcott on

"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