Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

show Image with CImage - Debug assertion failed

1,051 views
Skip to first unread message

Manuel Hoeger

unread,
Feb 16, 2010, 12:33:49 PM2/16/10
to
Hi,

I tried to show a png Image using CImage ( Visual Studio 2008 prof )

It works pretty good, but only in Release Mode. If I run it in Debug Mode
there is a Message:

"Debug Assertion Failed!
Program: ....[..]\application.exe
File: g:[...]\atlimage.h
Line: 503

Expression: m_hbitmap == 0
[..]


if I ignore the message the picture is schown. How can I avoid this Failure

My Code:

BOOL OK;
LPCTSTR lpBitSource=_T("D:\\Daten\\Visual Studio
2008\\Projects\\LitLaunchTestbox\\LitLaunch\\brett1.png");
CRect rect;
GetClientRect(&rect);

CWindowDC pDC(this);
CDC dc;
OK=m_Image.Load(lpBitSource);
dc.CreateCompatibleDC(&pDC);

m_Image.Draw(pDC.m_hDC,CRect(&rect));

Thanks for answers.

Manu


Seetharam

unread,
Feb 16, 2010, 4:20:42 PM2/16/10
to
It asserts because "m_Image" already has some value from the last
call.
Try calling "m_Image.Destroy()" after you are done with your drawing
or just before calling "m_Image.Load()" in your code above.

-Seetharam

Tom Serface

unread,
Feb 16, 2010, 10:57:37 PM2/16/10
to
To add to the other posts...

You will want to support any images that you create with the CImage class
(or any other GDI+ functions) otherwise you will get resource leaks.

Tom

"Manuel Hoeger" <Manuel...@physik.stud.uni-erlangen.de> wrote in message
news:7u037s...@mid.dfncis.de...

Joseph M. Newcomer

unread,
Feb 20, 2010, 1:24:18 AM2/20/10
to
See below...

On Tue, 16 Feb 2010 18:33:49 +0100, "Manuel Hoeger"
<Manuel...@physik.stud.uni-erlangen.de> wrote:

>Hi,
>
>I tried to show a png Image using CImage ( Visual Studio 2008 prof )
>
>It works pretty good, but only in Release Mode. If I run it in Debug Mode
>there is a Message:
>
>"Debug Assertion Failed!
>Program: ....[..]\application.exe
>File: g:[...]\atlimage.h
>Line: 503
>
>Expression: m_hbitmap == 0
>[..]

****
It is pretty clear what this means. It means that the m_hBitmap member of the class MUST
be NULL, and it ISN'T NULL. Why not? Because you already have an image attached to it!

Nowhere did I see where m_Image is freeing the image.

Which leads to the question of why, in your OnDraw/OnPaint handler, you are storing the
image which you are just loading into a class member, instead of a local variable.
****


>
>
>if I ignore the message the picture is schown. How can I avoid this Failure
>
>My Code:
>
>BOOL OK;
> LPCTSTR lpBitSource=_T("D:\\Daten\\Visual Studio
>2008\\Projects\\LitLaunchTestbox\\LitLaunch\\brett1.png");

****
I presume the goal here is that this program will run correctly only today, on your
machine, and you never expect it to run anywhere else. Otherwise, hardwiring a path like
this in represents a serious problem.
****


> CRect rect;
> GetClientRect(&rect);
>
> CWindowDC pDC(this);

****
Do you really want a window DC? Why? Wouldn't CClientDC dc(this) be more appropriate?
****
> CDC dc;
> OK=m_Image.Load(lpBitSource);
****
Once it is loaded, it is loaded forever, and any attempt to load it again generates the
error you see. You must explicitly delete this image with CImage::Destroy, e.g., after
you draw it, you must do
m_Image.Destroy();

but why does m_Image exist at all? A local variable would be perfect for this!
****
> dc.CreateCompatibleDC(&pDC);
>
> m_Image.Draw(pDC.m_hDC,CRect(&rect));
****
I am presuming this is in your OnDraw/OnPaint handler. If it isn't, all of the above code
is incorrect. It would be correct only if it were in the OnDraw/OnPaint handler.
joe
****
>
>Thanks for answers.
>
>Manu
>
>
>
Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

ngb...@gmail.com

unread,
Jul 10, 2018, 5:15:27 AM7/10/18
to
THANKYOU SO MUCH! you saved my day!
0 new messages