Click here to Skip to main content
11,477,625 members (77,145 online)
Click here to Skip to main content
Add your own
alternative version

Finding Undisposed Objects

, 13 Aug 2009 Ms-PL 99.3K 1.8K 234
An application to find undisposed objects in your .NET application.
#include "StdAfx.h"
#include "TypeTracker.h"

#include <sstream>
#include "assert.h"

TypeTracker::TypeTracker(void) : m_pClientData(NULL), m_pClassLoadedCallback(NULL)
{
}

void TypeTracker::Initialize(CComPtr<ICorProfilerInfo2> pCorProfilerInfo)
{
	m_pCorProfilerInfo = pCorProfilerInfo;
}

void TypeTracker::RegisterForClassLoad(ClassLoaded pClassLoadedFunc, void *data)
{
	m_pClientData = data;
	m_pClassLoadedCallback = pClassLoadedFunc;
}

HRESULT TypeTracker::ClassLoadFinished(ClassID classId,	HRESULT hrStatus)
{
	CLRClass clrClass;
	HRESULT hr = CreateAndAddToCache(classId, &clrClass);

	if (m_pClassLoadedCallback != NULL)
	{
		m_pClassLoadedCallback(clrClass, m_pClientData);
	}

	return hr;
}

HRESULT TypeTracker::GetCLRClassFor(ClassID classId, CLRClass *pCLRClass)
{
	//OutputDebugStringA("Gee ClassID");
	TYPES_TO_WATCH_ITERATOR iter = m_TypesCache.find(classId);

	//OutputDebugStringA("Find Completed");

	if (iter != m_TypesCache.end())
	{
		*pCLRClass = iter->second;
		return S_OK;
	}

	HRESULT hr = CreateAndAddToCache(classId, pCLRClass);

	//assert(SUCCEEDED(hr));

	/*std::ostringstream str;
	str << hr;
	OutputDebugStringA(str.str().c_str());*/

	return hr;
}

HRESULT TypeTracker::CreateAndAddToCache(ClassID classId, CLRClass *pCLRClass)
{
	//OutputDebugString(L"Before Loading Type");

	ModuleID moduleId = 0;
	mdTypeDef typeDefToken = 0;

	//ofs << "Loaded " << classId;
	//ofs.flush();

	HRESULT hr = m_pCorProfilerInfo->GetClassIDInfo(classId, &moduleId, &typeDefToken);

	//OutputDebugStringA("After GetClassIDInfo");

	if (FAILED(hr))
		return hr;

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

	if (FAILED(hr))
		return hr;

	WCHAR buf[512];
	ULONG typeNameLength;
	DWORD flags = 0;
	mdToken baseTypeDef = 0;

	hr = metaDataGetter->GetTypeDefProps(typeDefToken, buf, sizeof(buf)/sizeof(buf[0]), &typeNameLength, &flags, &baseTypeDef);

	if (FAILED(hr))
		return hr;

	CLRClass clrClass;
	clrClass.classId = classId;
	clrClass.metaDataToken = typeDefToken;
	clrClass.fullTypeName = std::wstring(buf);

	bool hasFinalizer = true;
	//if (SUCCEEDED(DoesTypeHaveFinalizer(classId, &hasFinalizer)))
		clrClass.hasFinalizer = hasFinalizer;

	m_TypesCache[classId] = clrClass;

	*pCLRClass = clrClass;

	//OutputDebugStringA("After Loading Type");
	return S_OK;
}

HRESULT TypeTracker::DoesTypeHaveFinalizer(CComPtr<IMetaDataImport2> pMetaDataGetter, ClassID classId, mdToken typeDefToken, bool *result)
{
	*result = true;
	return S_OK;
}

TypeTracker::~TypeTracker(void)
{
}

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