We help IT Professionals succeed at work.

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

aquila98
aquila98 asked
on
447 Views
Last Modified: 2013-11-20
Hi,

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
seclected...

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 );
       pDC->DPtoLP(&size);

       CPoint org(0,0);
       pDC->DPtoLP(&org);

       // 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 );
               pDC->RealizePalette();
       }
         ::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 ?????????

Thanks!
Comment
Watch Question

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

Author

Commented:
Hmmmmmmmmm.

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,
(HBITMAP)*pBitmap...)

and

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));
pp.SaveImage("toto.bmp");

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?

thanks!
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.

Author

Commented:
Sorry...

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

CBitmap* SECImage::MakeBitmap(CDC *pDC)
{
    ASSERT_VALID(pDC);

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

#ifdef WIN32
    ::StretchDIBits(
            pNewDC->GetSafeHdc(),
            0,0,
            m_dwWidth, m_dwHeight,
            0,0,
            m_dwWidth, m_dwHeight,
            m_lpSrcBits,
            m_lpBMI, DIB_RGB_COLORS,
            SRCCOPY);
#else
    ::StretchDIBits(
            pNewDC->GetSafeHdc(),
            0,0,
            (int)m_dwWidth, (int)m_dwHeight,
            0,0,
            (int)m_dwWidth, (int)m_dwHeight,
            m_lpSrcBits,
            m_lpBMI, DIB_RGB_COLORS,
            SRCCOPY);
#endif            

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

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

Author

Commented:
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?

Author

Commented:
No...

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

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

Weird!
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.

Author

Commented:
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
AFX_VALID_MANAGE_INSTANCE_OR_SOMETHING(dc)
function that would ensure I have a valid dc????

Thanks for your efforts ;-)

Author

Commented:
and yet...
i have this also being drawn in OnDraw()
pDC->TextOut(pt.left,
pt.bottom-1,pI->GetOriginalName().c_str());

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?

Author

Commented:
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)
UNLOCK SOLUTION

Author

Commented:
>(

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!)
LoadThumbnail....

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 ;-)



Author

Commented:
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.

OR

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.