Solved

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

Posted on 2000-02-16
15
396 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!
0
Comment
Question by:aquila98
  • 9
  • 6
15 Comments
 
LVL 4

Expert Comment

by:abancroft
ID: 2532214
Can't see anything wrong, but try checking some function return values to see where things are going wrong.
0
 
LVL 2

Author Comment

by:aquila98
ID: 2532332
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!
0
 
LVL 4

Expert Comment

by:abancroft
ID: 2532366
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.
0
 
LVL 2

Author Comment

by:aquila98
ID: 2532375
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????????????
0
 
LVL 2

Author Comment

by:aquila98
ID: 2532382
Yes, GetThumbnail works. If I save it in a file I see it ok.

So its really MakeBitmap that fails :(
0
 
LVL 4

Expert Comment

by:abancroft
ID: 2532383
How are you generating the device context you pass to all these functions?
0
 
LVL 2

Author Comment

by:aquila98
ID: 2532397
No...

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

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

Weird!
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 4

Expert Comment

by:abancroft
ID: 2532413
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.
0
 
LVL 2

Author Comment

by:aquila98
ID: 2532454
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 ;-)
0
 
LVL 2

Author Comment

by:aquila98
ID: 2532466
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????????
0
 
LVL 4

Expert Comment

by:abancroft
ID: 2532474
Then it's not the DC, but your XImage item.

Anyway to check it?
0
 
LVL 2

Author Comment

by:aquila98
ID: 2532517
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?
0
 
LVL 4

Accepted Solution

by:
abancroft earned 70 total points
ID: 2532532
OK.

Try this, just before the call to MakeBitmap():
CBitmap bmpTest;
bmpTest.CreateBitmap(32, 32, 1, 24, NULL);
CBitmap *pBmpPrev = pDC->SelectObject(&bmpTest);
(*bitmap)= dib->MakeBitmap(pDC);
(void)pDC->SelectObject(pBmpPrev);


0
 
LVL 2

Author Comment

by:aquila98
ID: 2532598
>(

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



0
 
LVL 2

Author Comment

by:aquila98
ID: 2532603
oops! forgot to give you points ;-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now