dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Assertion failure in load/setbitmap()

  1. #1
    Join Date
    Nov 2007
    Posts
    6

    Assertion failure in load/setbitmap()

    In my application (VS 2003) I want to display a bitmap in a picture control. I want to change the bitmap depending on what color mode the user has chosen. I've attached a CStatic variable to the picture control, but calling the SetBitmap raises an assertion failure. Initially the assertion was raised when I called the LoadBitmap on a member variable CBitmap, but changing to a local variable "solved" the problem. What am I missing? All help greatly appreciated.

    DDX_Control(pDX, IDC_PICCTRL, m_oPicCtrl);



    HBRUSH CMyClass:OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CGuiBase::OnCtlColor(pDC, pWnd, nCtlColor);

    // Set brush to desired background color
    CPalette * pPalette = (CPalette*)GetPalette();
    CBitmap bmp;
    int lightMode = m_iCurrentLightMode;
    GetLightMode(&lightMode, -1);
    if(lightMode != m_iCurrentLightMode || m_iCurrentLightMode < 0)
    {
    try
    {
    if(lightMode==BRIGHT_DAYLIGHT_DISP||lightMode==WHITE_DAY_LIGHT_DISP) //0
    {
    m_iCurrentLightMode = lightMode; bmp.LoadBitmap(IDB_BITMAP1);
    }
    else
    {
    m_iCurrentLightMode = lightMode;
    bmp.LoadBitmap(IDB_BITMAP");
    }
    m_oPicCtrl.SetBitmap(bmp)
    }

  2. #2
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,562

    Re: Assertion failure in load/setbitmap()

    Press the Retry button on the "Assertion failed..." dialog to step in the code to see where and why it happened.
    Victor Nijegorodov

  3. #3
    Join Date
    May 1999
    Location
    DELAWARE, USA
    Posts
    9,917

    Re: Assertion failure in load/setbitmap()

    It would be nice if you used CODE tags. It would have been much easier to read your code snippet.
    The question is: is your code snippet exact copy of the code you are using?
    I doubt it, since there are missing } brackets. That I can explain, since you wanted to show only part of the code.
    What troubles me is:
    Code:
    bmp.LoadBitmap(IDB_BITMAP");
    Is it a typo? With this your program would not compile.
    Possibly you are passing numeric constant and LoadBitmap fails because a bitmap has a name as a resource ID. It could be the other way around; hard to say.

    ***********
    Using local CBitmap did not resolve your problem. After OnCtlColor is out of scope bitmap object is gone. Each time OnCtlColor is invoked, local CBitmap object loads a bitmap. While when you used class member it loaded bitmap the very first OnCtlColor invocation and gave you an error since you cannot LoadBitmap to an object that already has one (bitmap handle is not NULL). You are getting assertion:
    Code:
     ASSERT(m_hObject == NULL);
    You could achieve the same result if you called DeleteObject on member variable before calling LoadBitmap.

    ***********
    You execute bitmap assigning code indiscriminately; WM_CTLCOLOR is send for every control so OnCtlColor handler is called frequently. You should perform check if WM_CTLCOLOR message was sent to static control you load bitmap.

    ***********
    Another possibility is that you create control later and window you expect has not been attached to a static control resulting with assertion. I doubt this is a case but it is a possibility.

    ***********
    In conclusion, I would use member variable of the CBitmap and load it with appropriate bitmap when m_iCurrentLightMode changes, assign it to a static control and force repainting.
    This way you would not have to call LoadBitmap and SetBitmap so frequently.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  4. #4
    Join Date
    Nov 2007
    Posts
    6

    Re: Assertion failure in load/setbitmap()

    Thanks for your reply

    Calling DeleteObject on my bitmap seems to work out that problem. Having looked at it a little closer, it seems to me that the problem might be that the window handler for the CStatic == 0?
    I thought this would be taken care of by the
    DDX_Control(pDX, IDC_PORTCANNONBMP, m_oPortCannonCtrl);
    DDX_Control(pDX, IDC_STBDCANNONBMP, m_oStbdCannonCtrl);
    (The IDC's being the ID of my picture boxes.)
    Or do I need to do something more to draw the static in my dialog window?
    If so, how do I set the handler correct?

    Here's an exact copy of the code:
    Code:
    HBRUSH CCntrPartDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
    	HBRUSH hbr = CGuiBase::OnCtlColor(pDC, pWnd, nCtlColor);
      
      // Set brush to desired background color
    	CPalette * pPalette = (CPalette*)GetPalette();
    	int iLightMode = m_iCurrentLightMode;
    	GetLightMode(&iLightMode, -1);
    	if(iLightMode != m_iCurrentLightMode || m_iCurrentLightMode < 0)
    	{
    		try
    		{
    			if(iLightMode==BRIGHT_DAYLIGHT_DISP||iLightMode==WHITE_DAY_LIGHT_DISP) //0
    			{
    				m_iCurrentLightMode = iLightMode;
    				m_oPortCannonBmp.DeleteObject();
    				m_oStbdCannonBmp.DeleteObject();
    				m_oPortCannonBmp.LoadBitmap(IDB_PORTCANNONDAY);
    				m_oStbdCannonBmp.LoadBitmap(IDB_STBDCANNONDAY);
    			}
    			else
    			{
    				m_iCurrentLightMode = iLightMode;
    				m_oPortCannonBmp.DeleteObject();
    				m_oStbdCannonBmp.DeleteObject();
    				m_oPortCannonBmp.LoadBitmap(IDB_PORTCANNONNIGHT);
    				m_oStbdCannonBmp.LoadBitmap(IDB_STBDCANNONNIGHT);
    			}
    			m_oPortCannonCtrl.SetBitmap(m_oPortCannonBmp);
    //			m_oPortCannonCtrl.Invalidate();
    			m_oStbdCannonCtrl.Attach((HWND)&pWnd);// 
    			m_oStbdCannonCtrl.SetBitmap(m_oStbdCannonBmp);
    		}
    		catch(...)
    		{
    			m_oStbdCannonBmp.Detach();
    		}
    	}	
    ...
    }
    DEbug info from the variables:
    + m_oStbdCannonCtrl {CStatic hWnd=0x0013a884 {unused=0 }} CStatic
    + m_oPortCannonCtrl {CStatic hWnd=0x019a06f8 {unused=0 }} CStatic

  5. #5
    Join Date
    Nov 2007
    Posts
    6

    Re: Assertion failure in load/setbitmap()

    Solved it (I think).
    Moving the entire code to the OnPaint() seemed to do the trick.

    Thanks again

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)