We help IT Professionals succeed at work.

A MDI app, CScrollView, in OnDraw works fine in debug, nothing displayed in release! WHY???

aquila98 asked
Last Modified: 2013-11-20

I have this application (MDI)... In it are views (CScrollView) that are
displayed using overloading of OnDraw.

In OnDraw I display a bunch of thumbnails contained in a STL set
(ie. set<string>, where string is filename)...
Each thumbnail get a RECT and tools are activated whenever
user click inside a rect...

In DEBUG, I see everything displayed and the tools match the picture

BUT in release NOTHING is displayed! Yet, the RECT is set because
I can select an image (tools adjusted)! It's just that it doesn't
get displayed!!!!!!! Any idea why? And how to fit it?

What could be missing in release that is there in debug???

here is the code of the DrawBitmap function used to display the
image. Any hints as to what is going wrong will be appreciated ;-)

void CMyView::DrawBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, int xDest, int yDest, int width, int height, bool selected )
       // Get logical coordinates
       BITMAP bm;
       ::GetObject( hBitmap, sizeof( bm ), &bm );
       CPoint size( bm.bmWidth, bm.bmHeight );

       CPoint org(0,0);

       // Create a memory DC compatible with the destination DC
       CDC memDC;
       memDC.CreateCompatibleDC( pDC );
       memDC.SetMapMode( pDC->GetMapMode() );
       //memDC.SelectObject( &bitmap );
       HBITMAP hBmOld = (HBITMAP)::SelectObject( memDC.m_hDC, hBitmap );
       // Select and realize the palette
       if( hPal && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
               SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );
         ::SetStretchBltMode(memDC.m_hDC, COLORONCOLOR);
         //pDC->StretchBlt(xDest, yDest, width, height, &memDC, 0,0, bm.bmWidth, bm.bmHeight, SRCCOPY );
       //not stretched but cropped
         pDC->BitBlt(xDest, yDest, width, height /*size.x, size.y*/, &memDC, 0,0 /*org.x, org.y,*/, (selected) ? SRCINVERT:SRCCOPY);

       ::SelectObject( memDC.m_hDC, hBmOld );

What's going on in there ?????????

Watch Question

Can't see anything wrong, but try checking some function return values to see where things are going wrong.



I narrow it down somewhat...

Its the HBITMAP that does not seems to be ok in
release! Here it is:
void CMyView::DrawBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, int xDest, int yDest, int width,
                int height, bool selected )
                       // Get logical coordinates
                       BITMAP bm;
                       ::GetObject( hBitmap, sizeof( bm ), &bm );

the  bm.bmWidth, bm.bmHeight  are 1 in release and
are correct in debug!

Now this HBITMAP comes from this code:

CBitmap* pBitmap= NULL;
CPalette* pPalette= NULL;
GetBitmapAndPalette(theImage, &pBitmap, &pPalette, pDC);

and then of course CMyView::DrawBitmap(pDC,


BOOL CMyView::GetBitmapAndPalette(XImage* pImage, CBitmap **bitmap, CPalette **pal, CDC* pDC)
      SECJpeg* dib= pImage->GetThumbnail();

      if (dib != NULL)
            (*bitmap)= dib->MakeBitmap(pDC);

Now SECJpeg is a cless from ObjectiveToolkit (stingray's)
and this call to dib->MakeBitmap(pDC); works only
in debug! In release it does not! WHY?????????????

Further, if I put this code:
SECDib pp;
pp.CreateFromBitmap(pDC, (*bitmap));

the file "toto.bmp" is ok in debug but of size 1k in release!

So this is the culprit :SECImage::MakeBitmap()
But why does it fail in release and not in debug????

Anyone familiar with stingray?

I used it (objective toolkit 6.02) because their classes
had a nice wrapper to jpg and bmp but it does not
work :(

Do you have any idea why the function fails in release?

Before the call to GetBitmapAndPalette(), is there any way to verify that pImage is valid? Maybe it's been corrupted before the call.

Also, is there any way to check that pImage->GetThumbnail() is working correctly?

When I come across a bug like this, I usually try commenting out code (or replacing it with simpler code) until the problem goes away. That helps narrow down the bug some more.

Another approach is to generate an app (using AppWizard) and gradually introduce your problem code until the bug appears.



Might as well put in the code from MakeBitmap!
Here it is:

CBitmap* SECImage::MakeBitmap(CDC *pDC)

    CBitmap* pBitmap = new CBitmap();
      CBitmap* pBitmap2;
      CDC *pNewDC = new CDC();
#ifdef WIN32
            m_dwWidth, m_dwHeight);
            (int)m_dwWidth, (int)m_dwHeight);
      pBitmap2 = pNewDC->SelectObject(pBitmap);

#ifdef WIN32
            m_dwWidth, m_dwHeight,
            m_dwWidth, m_dwHeight,
            m_lpBMI, DIB_RGB_COLORS,
            (int)m_dwWidth, (int)m_dwHeight,
            (int)m_dwWidth, (int)m_dwHeight,
            m_lpBMI, DIB_RGB_COLORS,

      pBitmap2 = (CBitmap *)pNewDC->SelectObject(pBitmap2);
      delete pNewDC;
      return pBitmap2;
    // return pBitmap;

Now why would this not work in release????????????


Yes, GetThumbnail works. If I save it in a file I see it ok.

So its really MakeBitmap that fails :(
How are you generating the device context you pass to all these functions?



Its all being done in void CMyView::OnDraw(CDC* pDC)

So its the dc I get from the view some where...

Hmmm...when a DC is created it has a 1x1 monochrome bitmap in it.

If this is the case when it's passed to OnDraw() (and I'm not 100% sure about this) then the call to CreateCompatibleBitmap() in SECImage::MakeBitmap() will create the same thing....

Try using the DC from GetDC() & passing it to MakeBitmap(). Call ReleaseDC() afterwards.


Yes... I read that somewhere about the 1X1 bitmap ;-)

Except, using GetDC() did not changed anything!

How could the DC from OnDraw() be corrupted in
release and not in debug? How can I test a valid DC???

If there a kind of
function that would ensure I have a valid dc????

Thanks for your efforts ;-)


and yet...
i have this also being drawn in OnDraw()

and I see this filename correctly displayed in debug AND
release! So the DC seems to be corrupted ONLY in the
case of a CBitmap object????????
Then it's not the DC, but your XImage item.

Anyway to check it?


the XImage must be good since I can get its
thumbnail (a SECJpeg*) and save to disk in a file
I can see if ok (acdsee displays it)...

so its back to MakeBitmap that somehow builds a
monochrome bitmap of size 1X1 from the DC
with a valid image.

This dc is good when I draw text but not when I
draw bitmaps.

Something happens to the dc (?) when the object
is a CBitmap and were in release that does not in debug!
But what?

Its getting hopeless?
This one is on us!
(Get your first solution completely free - no credit card required)



I did put in your code and it changed nothing...
But i decided to clean and rebuild all my app

and now I get finally an error in (you guessed it!)

Seems devstudio was mixed up and did not compiled
the project properly (it's in a dll)...

I think a VERIFY insterd of an ASSERT will ensure
the function is properly called both in debug and in release
and now that I have an error that makes sense I can go on
on my own ;-)

Thank you for your time ;-)


oops! forgot to give you points ;-)
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.