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

Thread: Resource Exception

  1. #1
    Join Date
    Nov 2011
    Location
    India
    Posts
    179

    Resource Exception

    Hi,

    I used onctlcolor() method to set the background color for the controls & Dialog.

    i run my application, working good but after 15 mins the below error occured,

    Application Error:

    The Required Resource Was --------> Shown message box

    Program Error:

    First-chance exception at 0x7c81eb33 in XRay.exe: Microsoft C++ exception: CResourceException @ 0x001274f8.
    First-chance exception at 0x7c81eb33 in XRay.exe: Microsoft C++ exception: CResourceException @ 0x001259d0.
    First-chance exception at 0x7c81eb33 in XRay.exe: Microsoft C++ exception: CResourceException @ 0x00123ea8.
    First-chance exception at 0x7c81eb33 in XRay.exe: Microsoft C++ exception: CResourceException @ 0x00122380.
    First-chance exception at 0x7c81eb33 in XRay.exe: Microsoft C++ exception: CResourceException @ 0x00120858.
    Warning: Uncaught exception in WindowProc (returning 0).

    what is the reason for that?

    How can i prevent my project from this problem.
    Regards,

    SaraswathiSrinath

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: Resource Exception

    Sounds like you leak some GDI resources. Could you show your actual code in onctlcolor() method?
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2011
    Location
    India
    Posts
    179

    Re: Resource Exception

    But i never used any sound files in my project. For your reference code in onctlcolor() method,

    HBRUSH CXRayFIDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    if( pWnd->GetDlgCtrlID() == IDC_STATIC1 || pWnd->GetDlgCtrlID() == IDC_STATIC2
    || pWnd->GetDlgCtrlID() == IDC_STATIC3 || pWnd->GetDlgCtrlID() == IDC_STATIC4
    || pWnd->GetDlgCtrlID() == IDC_STATIC5 || pWnd->GetDlgCtrlID() == IDC_STATIC6
    || pWnd->GetDlgCtrlID() == IDC_STATIC7 || pWnd->GetDlgCtrlID() == IDC_STATIC8
    || pWnd->GetDlgCtrlID() == IDC_STATIC9 || pWnd->GetDlgCtrlID() == IDC_STATIC10 ||
    pWnd->GetDlgCtrlID() == IDC_STATIC11 || pWnd->GetDlgCtrlID() == IDC_STATIC12 ||
    pWnd->GetDlgCtrlID() == IDC_STATIC13 || pWnd->GetDlgCtrlID() == IDC_STATIC14 ||
    pWnd->GetDlgCtrlID() == IDC_STATIC15 || pWnd->GetDlgCtrlID() == IDC_STATIC16 ||
    pWnd->GetDlgCtrlID() == IDC_STATIC17 || pWnd->GetDlgCtrlID() == IDC_STATIC19 ||
    pWnd->GetDlgCtrlID() == IDC_STATIC20 || pWnd->GetDlgCtrlID() == IDC_STATIC ||
    pWnd->GetDlgCtrlID() == IDC_TEXT1 || pWnd->GetDlgCtrlID() == IDC_TEXT2 ||
    pWnd->GetDlgCtrlID() == IDC_TEMPEXT)
    {
    pDC->SetTextColor(RGB(128, 255, 0));
    pDC->SetBkColor(RGB(0,0,0));
    pDC->SetBkMode(TRANSPARENT);
    HBRUSH B = CreateSolidBrush(RGB(0,0,0));
    return (HBRUSH) B;
    }

    if (nCtlColor != CTLCOLOR_EDIT && nCtlColor != CTLCOLOR_LISTBOX)
    {
    pDC->SetBkMode(TRANSPARENT);
    HBRUSH B = CreateSolidBrush(RGB(65,65,65));
    return (HBRUSH) B;
    }
    return hbr;
    }
    Regards,

    SaraswathiSrinath

  4. #4
    Join Date
    Apr 1999
    Posts
    27,444

    Re: Resource Exception

    Quote Originally Posted by saraswathisrinath View Post
    But i never used any sound files in my project. For your reference code in onctlcolor() method,
    What does GDI resources have to do with creating sound files?

    Second, where is the call to DeleteObject for those brushes you created? Please read the documentation on CreateSolidBrush. Without the call to DeleteObject, you're creating these brushes and not deleting them:

    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    Also, after 43 posts, it should have been clear to use code tags when posting code. Your code is unformatted
    Code:
    HBRUSH CXRayFIDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
    {
        HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
        if( pWnd->GetDlgCtrlID() == IDC_STATIC1 || pWnd->GetDlgCtrlID() == IDC_STATIC2
        || pWnd->GetDlgCtrlID() == IDC_STATIC3 || pWnd->GetDlgCtrlID() == IDC_STATIC4
        || pWnd->GetDlgCtrlID() == IDC_STATIC5 || pWnd->GetDlgCtrlID() == IDC_STATIC6
        || pWnd->GetDlgCtrlID() == IDC_STATIC7 || pWnd->GetDlgCtrlID() == IDC_STATIC8
        || pWnd->GetDlgCtrlID() == IDC_STATIC9 || pWnd->GetDlgCtrlID() == IDC_STATIC10 || 
        pWnd->GetDlgCtrlID() == IDC_STATIC11 || pWnd->GetDlgCtrlID() == IDC_STATIC12 ||
        pWnd->GetDlgCtrlID() == IDC_STATIC13 || pWnd->GetDlgCtrlID() == IDC_STATIC14 ||
        pWnd->GetDlgCtrlID() == IDC_STATIC15 || pWnd->GetDlgCtrlID() == IDC_STATIC16 || 
        pWnd->GetDlgCtrlID() == IDC_STATIC17 || pWnd->GetDlgCtrlID() == IDC_STATIC19 || 
        pWnd->GetDlgCtrlID() == IDC_STATIC20 || pWnd->GetDlgCtrlID() == IDC_STATIC || 
        pWnd->GetDlgCtrlID() == IDC_TEXT1 || pWnd->GetDlgCtrlID() == IDC_TEXT2 ||
        pWnd->GetDlgCtrlID() == IDC_TEMPEXT) 
        {
            pDC->SetTextColor(RGB(128, 255, 0));
            pDC->SetBkColor(RGB(0,0,0));
            pDC->SetBkMode(TRANSPARENT);
            HBRUSH B = CreateSolidBrush(RGB(0,0,0) ); 
            return (HBRUSH) B;
        }
    //...
    See the difference in the code tags?

    As to your code, why are you repeatedly calling GetDlgCtrlID()? Just call it once, save the return value into an int variable, and just use that variable to compare.

    Also, you could have just placed the ID's you care about in an array, and then search the array to see if any of them match the return value of GetDlgCtrlID(). This will make the code much easier to maintain instead of having so many comparisons.
    Code:
    //...
    const int MyID[] = {IDC_STATIC1, IDC_STATIC2, IDC_STATIC3 /* etc */ };
    const int numIDS = sizeof(MyID) / sizeof(MyID[0]);
    //...
    int idToFind = pWnd->GetDlgCtrlID();
    bool bFoundID = false;
    int foundID = -1;
    for (int i = 0; i < numIDS && !bFoundID; ++i)
    {
        if ( idToFind == MyID[i] )
        {
             bFoundID = true;
             foundID = MyID[i];
        }
    }
    
    if ( bFoundID )
    {
        // do something with the found ID
    }
    With the code above, all you need to do is to add the new ID to the array at the top. There is no need for more and more comparisons.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 7th, 2013 at 03:03 AM.

  5. #5
    Join Date
    Nov 2011
    Location
    India
    Posts
    179

    Re: Resource Exception

    1. I didn't create any sound files.

    2. This is my first project in MFC, I'm learning from google, codegure, msdn & code project web site only.
    Just i search my project requirement from net & i used the code to my project. so i never known about DeleteObject? Sure I will read the
    MSDN page & will use the DeleteObject method in my project. here after I will use code tags when posting code.

    3. I taken the code from google how to set background color for dialog, so i used that code for all my dialog controls(GetDlgCtrlID()).
    I don't know this array method.I will use the control ID array method in my onctlcolor method.

    4. For using Control ID Array & DeleteObject method, shall i recover my project from resource Exception problem sir?

    I will try the above 2 methods and come back with you sir.
    Regards,

    SaraswathiSrinath

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: Resource Exception

    Quote Originally Posted by VictorN View Post
    Sounds like you leak some GDI resources. Could you show your actual code in onctlcolor() method?
    Quote Originally Posted by saraswathisrinath View Post
    But i never used any sound files in my project.
    Dear SaraswathiSrinath,
    "Sounds" in this context means "it looks like ...", so your program behavior "looks like" there is a GDI resources leak.

    Quote Originally Posted by saraswathisrinath View Post
    3. I taken the code from google how to set background color for dialog, so i used that code for all my dialog controls(GetDlgCtrlID()).
    You may want to look at this very interesting and useful essay:
    Changing Dialog Box/CFormView Background Color
    Victor Nijegorodov

  7. #7
    Join Date
    Nov 2011
    Location
    India
    Posts
    179

    Re: Resource Exception

    Thanks Mr.Victor & Mr.Paul McKenzie .

    I solved my problem by your valuable post. Really feeling very happy. Thanks a lot.

    Code:
    const COLORREF MYCOLOR = RGB(0,0,0);
            CBrush brush;
             
            BEGIN_MESSAGE_MAP(CXRayDlg, CDialog)
            ON_WM_ERASEBKGND()
            ON_WM_CTLCOLOR()
            END_MESSAGE_MAP()
             
            BOOL CXRayDlg::OnEraseBkgnd(CDC* pDC)
            { 
                    CRect rcClient;
                    GetClientRect(&rcClient);
             
                    if((HBRUSH)brush == NULL)
                    brush.CreateSolidBrush(MYCOLOR);
             
                    pDC->FillRect(&rcClient, &brush);
                    brush.DeleteObject();
                    return TRUE;
            } 
            HBRUSH CXRayDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
            {
            HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
                    if((HBRUSH)brush == NULL)
                    brush.CreateSolidBrush(MYCOLOR);
                     
                    //...Store Controls ID in a array for set bkcolor & text color
                    const int MyID[] = {IDC_STATIC1,IDC_STATIC2,IDC_STATIC3};
                    const int numIDS = sizeof(MyID) / sizeof(MyID[0]);
                    //...
                    int idToFind = pWnd->GetDlgCtrlID();
                    bool bFoundID = false;
                    int foundID = -1;
                    for (int i = 0; i < numIDS && !bFoundID; ++i)
                    {
                    if ( idToFind == MyID[i] )
                    {
                    bFoundID = true;
                    foundID = MyID[i];
                    }
                    }
                    if ( bFoundID)
                    { 
                    pDC->SetTextColor(RGB(128, 255, 0));
                    pDC->SetBkColor(RGB(0,0,0));
                    pDC->SetBkMode(TRANSPARENT); 
                     
                    return (HBRUSH) brush;
                    }
                    brush.DeleteObject();
                    return hbr;
                     
                    }
    Last edited by saraswathisrinath; January 8th, 2013 at 11:23 PM.
    Regards,

    SaraswathiSrinath

  8. #8
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,057

    Re: Resource Exception

    Just an aside remark and advice.
    No offense, your CXRayFIDlg::OnCtlColor looks like a mess, then it's no wonder that you can easily make mistakes in it. Too much code, too many conditions, too many hardcoded color values. What about if you have tens of dialogs with tens of colored controls? And what about if in other places you need different colors and/or you have to change colors at run-time? Normally, will result a triple mess...

    First to note: MFC "reflects" WM_CTLCOLOR notification messages to the child controls. That allows handling them in a more object-oriented manner.
    So, much more reliable, flexible and easy to maintain is to derive your own control class (ex. CColoredStatic derived from CStatic) an handle reflected WM_CTLCOLOR messages.

    Here is an example of "colored" static control class:
    Code:
    // ColoredStatic.h
    
    class CColoredStatic : public CStatic
    {
       DECLARE_DYNAMIC(CColoredStatic)
    
    // Construction
    public:
       CColoredStatic();
    
    // Attributes
    private:
       int m_nBkMode;
       COLORREF m_crTextColor;
       COLORREF m_crBkColor;
       CBrush m_brush;
    
    // Operations
    public:
       void SetBkMode(int nValue);
       void SetTextColor(COLORREF crValue);
       void SetBkColor(COLORREF crValue);
    
    // Message handlers
    protected:
       DECLARE_MESSAGE_MAP()
       afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
    };
    Code:
    // ColoredStatic,cpp
    
    #include "stdafx.h"
    #include "ColoredStatic.h"
    
    // CColoredStatic
    IMPLEMENT_DYNAMIC(CColoredStatic, CStatic)
    
    CColoredStatic::CColoredStatic() 
    : m_nBkMode(OPAQUE),
      m_crTextColor(::GetSysColor(COLOR_WINDOWTEXT)),
      m_crBkColor(::GetSysColor(COLOR_BTNFACE))
    {
    }
    
    void CColoredStatic::SetBkMode(int nValue)
    {
       ASSERT((OPAQUE == nValue) || (TRANSPARENT == nValue));
       m_nBkMode = nValue;
       Invalidate();
    }
    
    void CColoredStatic::SetTextColor(COLORREF crValue)
    {
       m_crTextColor = crValue;
       Invalidate();
    }
    
    void CColoredStatic::SetBkColor(COLORREF crValue)
    {
       m_crBkColor = crValue;
       Invalidate();
    }
    
    BEGIN_MESSAGE_MAP(CColoredStatic, CStatic)
       ON_WM_CTLCOLOR_REFLECT()
    END_MESSAGE_MAP()
    
    // CColoredStatic message handlers
    
    HBRUSH CColoredStatic::CtlColor(CDC* pDC, UINT nCtlColor)
    {
       if(NULL != m_brush.GetSafeHandle())
          m_brush.DeleteObject();
    
       m_brush.CreateSolidBrush(m_crBkColor);
    
       pDC->SetBkMode(m_nBkMode);
       pDC->SetTextColor(m_crTextColor);
       pDC->SetBkColor(m_crBkColor);
    
       return m_brush;
    }
    All you have now to do in the parent dialog class is to include "ColoredStatic.h" and replace CStatic with CColoredStatic then call the methods for setting the color atributes whenever is necessary.
    Example:
    Code:
    #include "ColoredStatic.h"
    
    class CWhateverDialog : public CDialog
    {
       CColoredStatic m_static1;
       CColoredStatic m_static2;
       // ...
    };
    
    BOOL CWhateverDialog ::OnInitDialog()
    {
       CDialog::OnInitDialog();
    
       m_static1.SetBkMode(TRANSPARENT);
       m_static1.SetTextColor(RGB(0,0,255));
       
       m_static2.SetTextColor(RGB(255,255,255));
       m_static2.SetBkColor(RGB(255,0,0));
       
       // ...
    
       return TRUE;
    }
    In the same way you can proceed for other control types (edit and so on).
    It takes no much time to implement and test such class, once and forever, then easily use it anywhere is needed.
    Last edited by ovidiucucu; January 10th, 2013 at 05:39 AM.
    Ovidiu Cucu
    "When in Rome, do as Romans do."
    Visit: Microsoft Virtual Academy
    Follow: https://twitter.com/#!/ovidiucucu
    My blog: http://codexpert.ro/blog/author/ovidiu-cucu/

  9. #9
    Join Date
    Nov 2011
    Location
    India
    Posts
    179

    Re: Resource Exception

    Thank you . I will Replace this code & use.

    My requirement is , My dialog box background colour is black [ RGB(0, 0, 0) ] & Text colour is Green [ RGB(128, 255, 0) ].

    So i try to change the all controls edit box, picture box & button, etc.. colours into the same dialog box's background colour (Black).

    And also I'm used the same background colour & text colour for all my child dialog & controls in my project.
    Regards,

    SaraswathiSrinath

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: Resource Exception

    I gave you a link to the "Changing Dialog Box/CFormView Background Color" essay (in the post#6.
    Di you look at it?
    Victor Nijegorodov

  11. #11
    Join Date
    Nov 2011
    Location
    India
    Posts
    179

    Re: Resource Exception

    Yes victor sir.

    I first tried Ur link and reply (in the post#7).

    My problem solved

    I used Mr.Paul McKenzie sir code for avoid continously using GetDlgCtrlID() & used

    Code:
    if((HBRUSH)brush == NULL)
            brush.CreateSolidBrush(MYCOLOR);
    from Ur link. Really the link was very helped to understand my problem.

    Then only i try to used Mr.ovidiucucu's sir code.

    Both are working good with out any error(Resource Exception).

    Thanks a lot.
    Last edited by saraswathisrinath; January 15th, 2013 at 03:57 AM.
    Regards,

    SaraswathiSrinath

  12. #12
    Join Date
    Nov 2011
    Location
    India
    Posts
    179

    Re: Resource Exception

    Hi Victor,

    Your Code is Amazing. Really...

    Three solutions are working good. But Compare to other solution, this code is very short.

    Here I upload my code for who one have resource exception,

    Code:
    HBRUSH CDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
    {
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
    TCHAR classname[MAX_PATH];
    if(::GetClassName(pWnd->m_hWnd, classname, MAX_PATH) == 0)
    return hbr;
    if(_tcsicmp(classname, _T("BUTTON")) == 0)  //Don't set the Background colour for my button
    return hbr;
    
    pDC->SetBkColor(MYCOLOR);
    pDC->SetTextColor(RGB(128, 255, 0));
    
    if((HBRUSH)brush == NULL)
    brush.CreateSolidBrush(RGB(0,0,0));
    return (HBRUSH) brush;
    }
    
    BOOL CDlg::OnEraseBkgnd(CDC* pDC)
    { 
    CRect rcClient;
    GetClientRect(&rcClient);
    if((HBRUSH)brush == NULL)
    brush.CreateSolidBrush(MYCOLOR);
    pDC->FillRect(&rcClient, &brush);
    brush.DeleteObject();
    return TRUE;
    }
    Thanks Victor
    Regards,

    SaraswathiSrinath

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: Resource Exception

    Quote Originally Posted by saraswathisrinath View Post
    Here I upload my code for who one have resource exception,
    Please, upload ONLY properly formatted code (with indentations and so on).
    The one you posted is absolutely unreadable.
    Victor Nijegorodov

Tags for this Thread

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

This is a CodeGuru survey question.


Featured


HTML5 Development Center