My favorites | Sign in
Logo
             
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* Copyright (c) 2007-2008 SlimDX Group
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "ObjectTable.h"
#include "InternalHelpers.h"
#include "ComObject.h"

using namespace System;
using namespace System::Globalization;
using namespace System::Collections::Generic;
using namespace System::Diagnostics;

namespace SlimDX
{
static ObjectTable::ObjectTable()
{
m_Table = gcnew System::Collections::Generic::Dictionary<IntPtr, ObjectRecord>();

AppDomain::CurrentDomain->DomainUnload += gcnew System::EventHandler( OnExit );
AppDomain::CurrentDomain->ProcessExit += gcnew System::EventHandler( OnExit );
}

ObjectTable::ObjectTable()
{
}

void ObjectTable::OnExit( Object^ sender, EventArgs^ e )
{
SLIMDX_UNREFERENCED_PARAMETER(sender);
SLIMDX_UNREFERENCED_PARAMETER(e);

String^ leakString = ReportLeaks();
Debug::Write( leakString );
}

ComObject^ ObjectTable::Construct( IntPtr nativeObject )
{
if( nativeObject == IntPtr::Zero )
throw gcnew ArgumentNullException( "nativeObject" );

if( m_Table->ContainsKey( nativeObject ) )
{
return m_Table[nativeObject].Handle;
}

return nullptr;
}

void ObjectTable::Add( ComObject^ obj )
{
ObjectRecord record;
record.Handle = obj;

if( Configuration::EnableObjectTracking )
record.Source = gcnew StackTrace( 2, true );

m_Table->Add( obj->ComPointer, record );
}

void ObjectTable::Remove( ComObject^ obj )
{
m_Table->Remove( obj->ComPointer );
}

String^ ObjectTable::ReportLeaks()
{
String^ output = "";

for each( KeyValuePair<IntPtr, ObjectRecord> pair in m_Table )
{
if( pair.Value.Source == nullptr )
continue;

output += String::Format( CultureInfo::InvariantCulture, "Object of type {0} was not disposed. Stack trace of object creation:\n", pair.Value.Handle->GetType() );
for each( StackFrame^ frame in pair.Value.Source->GetFrames() )
{
if( frame->GetFileLineNumber() == 0 )
{
// Compiler autogenerated functions and the like can cause stack frames with no info;
// that's the only time the line number is 0 and since it's not a useful frame to see,
// we'll skip it
continue;
}

output += String::Format( CultureInfo::InvariantCulture, "\t{0}({1},{2}): {3}\n",
frame->GetFileName(),
frame->GetFileLineNumber(),
frame->GetFileColumnNumber(),
frame->GetMethod() );
}
}

output += String::Format( CultureInfo::InvariantCulture, "Total of {0} objects still alive.\n", m_Table->Count );
return output;
}
}
Show details Hide details

Change log

r389 by promit.roy on Feb 27, 2008   Diff
Adding missing files.
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 3644 bytes, 115 lines
Hosted by Google Code