Click here to Skip to main content
11,496,146 members (1,600 online)
Click here to Skip to main content
Add your own
alternative version

Finding Undisposed Objects

, 13 Aug 2009 Ms-PL 99.7K 1.8K 234
An application to find undisposed objects in your .NET application.
The site is currently in read-only mode for maintenance. Posting of new items will be available again shortly.
#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.

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