Discussion:
show Image with CImage - Debug assertion failed
(too old to reply)
Manuel Hoeger
2010-02-16 17:33:49 UTC
Permalink
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
2010-02-16 21:20:42 UTC
Permalink
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
n***@gmail.com
2018-07-10 09:15:25 UTC
Permalink
Post by Seetharam
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
THANKYOU SO MUCH! you saved my day!

Tom Serface
2010-02-17 03:57:37 UTC
Permalink
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
Post by Manuel Hoeger
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
"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
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
Joseph M. Newcomer
2010-02-20 06:24:18 UTC
Permalink
See below...
On Tue, 16 Feb 2010 18:33:49 +0100, "Manuel Hoeger"
Post by Manuel Hoeger
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
"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.
****
Post by Manuel Hoeger
if I ignore the message the picture is schown. How can I avoid this Failure
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.
****
Post by Manuel Hoeger
CRect rect;
GetClientRect(&rect);
CWindowDC pDC(this);
****
Do you really want a window DC? Why? Wouldn't CClientDC dc(this) be more appropriate?
****
Post by Manuel Hoeger
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!
****
Post by Manuel Hoeger
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
****
Post by Manuel Hoeger
Thanks for answers.
Manu
Joseph M. Newcomer [MVP]
email: ***@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
Loading...