Click here to Skip to main content
13,145,464 members (47,333 online)
Click here to Skip to main content

Stats

128.9K views
2.3K downloads
235 bookmarked
Posted 20 Jul 2009

Finding Undisposed Objects

, 13 Aug 2009
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)

Share

About the Author

S. Senthil Kumar
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.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170915.1 | Last Updated 13 Aug 2009
Article Copyright 2009 by S. Senthil Kumar
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid