Click here to Skip to main content
15,891,513 members
Articles / Desktop Programming / Windows Forms

Finding Undisposed Objects

Rate me:
Please Sign up or sign in to vote.
4.93/5 (105 votes)
13 Aug 2009Ms-PL5 min read 196.8K   2.9K   235  
An application to find undisposed objects in your .NET application.
#include "StdAfx.h"
#include "StackWalker.h"

StackWalker::~StackWalker(void)
{
}

HRESULT StackWalker::WalkStack(CComPtr<ICorProfilerInfo2> pCorProfilerInfo)
{
	//OutputDebugStringA("StackWalk Start");
	m_pCorProfilerInfo = pCorProfilerInfo;	

	HRESULT hr = m_pCorProfilerInfo->DoStackSnapshot(NULL, StackWalkCallback, COR_PRF_SNAPSHOT_DEFAULT, this, NULL, 0);

	//OutputDebugStringA("StackWalk End");

	return hr;
}

STDMETHODIMP StackWalker::StackWalkCallback(FunctionID funcId, UINT_PTR ip, 
															 COR_PRF_FRAME_INFO frameInfo, 
															 ULONG32 contextSize, 
															 BYTE context[], void *clientData)
{
	if (funcId == 0)
	{
		// Unmanaged code, ignore
		return S_OK;
	}

	StackWalker *pStackWalker = static_cast<StackWalker *>(clientData);

	const ULONG32 typeArgsCount = 50;
	ULONG32 actualTypeArgsCount = 0;
	ClassID parentClassId;
	ModuleID moduleId;
	mdToken funcToken;

	ClassID typeArgs[typeArgsCount];
	
	HRESULT hr = pStackWalker->m_pCorProfilerInfo->GetFunctionInfo2(funcId, 
					frameInfo, &parentClassId, &moduleId, &funcToken, sizeof(typeArgs), &actualTypeArgsCount, typeArgs); 

	if (FAILED(hr))
		return E_FAIL;
	
	CComPtr<IMetaDataImport2> metaDataGetter = NULL;
	hr = pStackWalker->m_pCorProfilerInfo->GetModuleMetaData(moduleId, ofRead, IID_IMetaDataImport2, (IUnknown **)&metaDataGetter);

	if (FAILED(hr))
		return E_FAIL;

	CLRClass clrClass;
	pStackWalker->m_TypeTracker.GetCLRClassFor(parentClassId, &clrClass);

	WCHAR buf[512] = {};
	ULONG actualSize = 0;
	DWORD attributes = 0;
	PCCOR_SIGNATURE signature;
	ULONG signatureLength;
	ULONG rva;
	DWORD flags;
	mdTypeDef classToken;

	metaDataGetter->GetMethodProps(funcToken, &classToken, buf, sizeof(buf), &actualSize, &attributes, &signature, &signatureLength, &rva, &flags);

	std::wstring qualifiedName = clrClass.fullTypeName + std::wstring(L"::") + std::wstring(buf);

	//pFinalizerRunDetector->ofs << qualifiedName << std::endl;

	pStackWalker->m_CurrentStack.push_back(qualifiedName);
	return S_OK;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Atmel R&D India Pvt. Ltd.
India India
I'm a 27 yrs old developer working with Atmel R&D India Pvt. Ltd., Chennai. I'm currently working in C# and C++, but I've done some Java programming as well. I was a Microsoft MVP in Visual C# from 2007 to 2009.

You can read My Blog here. I've also done some open source software - please visit my website to know more.

Comments and Discussions