mfc >> GDI resource management problem

by tim@DELETEMErobotcrazy.com » Tue, 28 Mar 2006 23:30:50 GMT

My App is throwing a resource exception within
CPen::CPen(style,width,colorref).

The App is drawing buttons onto two touch screens. Each touch screen
has 56 buttons. I have found that reducing the amount of buttons
increases the number of page refreshes before the resource exception is
thrown. I am assuming that the error is within the button drawing
code.

Below is an example of how I am drawing within the button classes. Is
there anything inherantly wrong with the code below which maybe leading
to resource leaks?

I am considering creating the GDI objects like brushes and pens as a
member. I only actually need two pens and two brushes for the whole
app. I am assuming I would increase performance if I did this also?

Any help, even scathing critique much appreciated!

==code

void CPepButton::DrawBorder(CMonitor &monitor, const
ButtonPositionIndex &nIndex,const bool isHighlighted)const
{
CMonitorDC dc( &monitor );

CBrush* pOldBrush = NULL;
CPen* pOldPen = NULL;

CPen orangePen(PS_SOLID,LINE_THICKNESS,ORANGE);

pOldPen = dc.SelectObject(&orangePen);

if(isHighlighted)
{
CBrush brDraw(ORANGE);
pOldBrush = dc.SelectObject(&brDraw);

//Draw the bounding box
dc.Rectangle( & m_ButtonRect);
brDraw.DeleteObject();
}
else
{
CBrush brDraw(BLACK);
pOldBrush = dc.SelectObject(&brDraw);

//Draw the bounding box
dc.Rectangle( & m_ButtonRect);
brDraw.DeleteObject();
}

//reselect GDI objects
if(pOldPen!=NULL)
{
dc.SelectObject(pOldPen);
}

if(pOldBrush!=NULL)
{
dc.SelectObject(pOldBrush);
}

ASSERT(pOldBrush->DeleteObject() );
ASSERT(pOldPen->DeleteObject() );
ASSERT(orangePen.DeleteObject() );

}



mfc >> GDI resource management problem

by AliR » Wed, 29 Mar 2006 00:29:17 GMT


You can't delete an object while it's selected in DC, you have to select the
old one back before you delete yours. What you are doing is deleting yours
before you select the old back in.

AliR.








mfc >> GDI resource management problem

by AliR » Wed, 29 Mar 2006 00:31:39 GMT

> ASSERT(pOldBrush->DeleteObject() );

I also noticed that you are deleting the object that came out of the DC when
you selected your objects in. Don't do that!!!.

AliR.







GDI resource management problem

by VSP » Wed, 29 Mar 2006 01:42:31 GMT

Better try using SaveDC/RestoreDC instead of Selecting the old pen/brushes
back.
There is no need to call the DeleteObject of pen/brush, the destructors will
call the DeleteObject.

Then it will look like

int iDC = SaveDC()

Select the gdi objects
drawing code

RestoreDC(iDC)

- VSP








Similar Threads

1. Disposal of GDI Resources and GDI Limits, a puzzling question (for me at least) - CSharp/C#

2. Visual Studio .NET garbles dialog size in resource view - NVidia Desktop Management problem

Just a post for anyone else that is having this problem (as I've now
discovered the solution).

The problem is where when looking in the dialog resource view of an
MFC project in Visual Studio .NET (2003 in my case) the dialog sizing
gets garbled and it chops off the edges of the dialog somehow and
renders the user unable to edit the dialog properly without screwing
the sizing up.

Well after months of having this problem I finally discovered that the
problem was happening because I was using the Desktop Management
feature of nView in the Nvidia drivers of my Nvidia card. Turning the
Desktop Management off fixed this problem. Typical Nvidia driver
quality.

In addition this was also causing me a problem where SQL Server Query
Analyzer would hang after doing virtually anything. Again, turning off
DM fixed this.

This was on Windows 2000 Pro, don't know whether this is a problem on
XP or not.

3. C# and GDI Resources - CSharp/C#

4. Windows resource/.rc, .h files and GDI Functions, Fonts & Bitmaps

Hi all,
I saw the following 2 files used in the OpenGL and DirectX 9 programming:
1) resource.h
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by dx9_indexed_geometry.rc
//
#define IDC_MYICON                      2
#define IDD_DIRECTX_DIALOG              102
#define IDD_ABOUTBOX                    103
#define IDS_APP_TITLE                   103
#define IDM_ABOUT                       104
#define IDM_EXIT                        105
#define IDS_HELLO                       106
#define IDI_DIRECTX_ICON                107
#define IDC_DIRECTX_MENU                108
#define IDC_DIRECTX_ACCELERATOR         109
#define IDC_DIRECTX                     110
#define IDR_MAINFRAME                   128
#define IDC_STATIC                      -1

// Next default values for new objects
// 
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE        129
#define _APS_NEXT_COMMAND_VALUE         32771
#define _APS_NEXT_CONTROL_VALUE         1000
#define _APS_NEXT_SYMED_VALUE           110
#endif
#endif
 2) filenameXYZ.rc  (opened by using Notepad)
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#define APSTUDIO_HIDDEN_SYMBOLS
#include "windows.h"
#undef APSTUDIO_HIDDEN_SYMBOLS
#include "resource.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (U.S.) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32

/////////////////////////////////////////////////////////////////////////////
//
// Icon
//

// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_DIRECTX_ICON        ICON                    "codesampler.ico"

/////////////////////////////////////////////////////////////////////////////
//
// Menu
//

IDC_DIRECTX_MENU MENU 
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit",                       IDM_EXIT
    END
    POPUP "&Help"
    BEGIN
        MENUITEM "&About ...",                  IDM_ABOUT
    END
END


/////////////////////////////////////////////////////////////////////////////
//
// Accelerator
//

IDC_DIRECTX_ACCELERATOR ACCELERATORS 
BEGIN
    "/",            IDM_ABOUT,              ASCII,  ALT, NOINVERT
    "?",            IDM_ABOUT,              ASCII,  ALT, NOINVERT
END


/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_ABOUTBOX DIALOG  22, 17, 230, 75
STYLE DS_SETFONT | DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
CAPTION "About"
FONT 8, "System"
BEGIN
    ICON            IDI_DIRECTX_ICON,IDC_MYICON,14,9,20,20
    LTEXT           "DirectX Sample v1.0",IDC_STATIC,49,10,119,8,SS_NOPREFIX
    LTEXT           "Copyright (C) 2001",IDC_STATIC,49,20,119,8
    DEFPUSHBUTTON   "OK",IDOK,195,6,30,11,WS_GROUP
END


#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

2 TEXTINCLUDE 
BEGIN
    "#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
    "#include ""windows.h""\r\n"
    "#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
    "#include ""resource.h""\r\n"
    "\0"
END

3 TEXTINCLUDE 
BEGIN
    "\r\n"
    "\0"
END

1 TEXTINCLUDE 
BEGIN
    "resource.h\0"
END

#endif    // APSTUDIO_INVOKED

#endif    // English (U.S.) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif    // not APSTUDIO_INVOKED
///////////////////////////////////////////////////////////////////////////
Issue #1: I do not know how the numerical numbers are decided and used in 1).
Issue #2: I have a hard to do the coding to create the .rc file like 2) for 
Icon, Menu, Accelerator, Dialog, TEXTINCLUDE, etc. 
Issue #3: Chapter 6 of Book "Programming with Microsoft Visual C++ .NET" 
(Core Reference) by G. Shepherd with D. Kruglinski teaches Classic GDI 
Functions, Fonts, and Bitmaps used in MFC.  I do not know whether these GDI 
Functions, Fonts and Bitmaps are related to the .rc file for Icon, Menu, 
Accelerator, Dialog, TEXTINCULE, etc.  Please help and address the 
above-mentioned 3 issues for me or point out where I can find the answers for 
them.  

Thanks in adanve,
SHC

5. GDI resource leak

6. MFC GDI Resource leak reintroduced in WinXP SP2?

7. FloatControlBar failure to release GDI resources

8. tool to find GDI resource leak

Hallo,

i search a tool or DLL or source to find GDI resource leaks.
I've Boundschecker, but it don't find GDI resource leaks.

	Jimmy