Knowledge Base Nr: 00316 gdileaks.cpp - http://www.swe-kaiser.de

Downloads:

win32/mfc: detect gdi resource leaks

  
class DbgGuiLeak
{
public:
explicit DbgGuiLeak(const CString sComment)
{
m_sComment = sComment;
m_nGuiResCount = ::GetGuiResources (::GetCurrentProcess(), GR_GDIOBJECTS);
}
~DbgGuiLeak()
{
int nLeaksGDI = ::GetGuiResources (::GetCurrentProcess(), GR_GDIOBJECTS) - m_nGuiResCount;
if (nLeaksGDI != 0)
{
TRACE("###> Gui Resources Leaked (%ws): %d\n", m_sComment, nLeaksGDI);
ASSERT(FALSE);
}
}
private:
CString m_sComment;
unsigned m_nGuiResCount;
};

#### erstes beispiel

DbgGuiLeak* g_pLeakDetect;

CscrollviewView::CscrollviewView()
{
g_pLeakDetect = new DbgGuiLeak(L"CscrollviewView");
}

CscrollviewView::~CscrollviewView()
{
delete g_pLeakDetect;
}


#### zweites beispiel

void CgditestView::OnDraw(CDC* /*pDC*/)
{
DbgGuiLeak(L"OnDraw");

CgditestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: Code zum Zeichnen der systemeigenen Daten hinzufügen
}