My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include "stdafx.h"
/*
* Copyright (c) 2007-2012 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::Text;
using namespace System::Threading;
using namespace System::Globalization;
using namespace System::Collections::ObjectModel;
using namespace System::Collections::Generic;
using namespace System::Diagnostics;

namespace SlimDX
{
static ObjectTable::ObjectTable()
{
m_Table = gcnew Dictionary<IntPtr, ComObject^>();
m_Ancillary = gcnew Dictionary<IntPtr, List<ComObject^>^>();
m_SyncObject = gcnew Object();

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::Find( IntPtr nativeObject )
{
Monitor::Enter( m_SyncObject );
try
{
if( m_Table->ContainsKey( nativeObject ) )
{
return m_Table[nativeObject];
}

return nullptr;
}
finally
{
Monitor::Exit( m_SyncObject );
}
}

bool ObjectTable::Contains( ComObject^ object )
{
Monitor::Enter( m_SyncObject );
try
{
return m_Table->ContainsKey( object->ComPointer );
}
finally
{
Monitor::Exit( m_SyncObject );
}
}

void ObjectTable::RegisterParent( ComObject^ object, ComObject^ owner )
{
if( owner != nullptr )
{
if( !m_Ancillary->ContainsKey( owner->ComPointer ) )
{
m_Ancillary->Add( owner->ComPointer, gcnew List<ComObject^>() );
}
m_Ancillary[owner->ComPointer]->Add( object );
}
}

void ObjectTable::Add( ComObject^ object, ComObject^ owner )
{
if( object == nullptr )
throw gcnew ArgumentNullException( "comObject" );

// Record tracking information
object->SetCreationTime( static_cast<int>( Configuration::Timer->ElapsedMilliseconds ) );
if( Configuration::EnableObjectTracking )
object->SetSource( gcnew StackTrace( 2, true ) );

// Add to the table
Monitor::Enter( m_SyncObject );
try
{
m_Table->Add( object->ComPointer, object );
RegisterParent( object, owner );

ObjectAdded( nullptr, gcnew ObjectTableEventArgs( object ) );
}
finally
{
Monitor::Exit( m_SyncObject );
}
}

bool ObjectTable::Remove( ComObject^ object )
{
if( object == nullptr )
throw gcnew ArgumentNullException( "comObject" );

Monitor::Enter( m_SyncObject );
try
{
if( !m_Table->ContainsKey( object->ComPointer ) )
return false;

m_Table->Remove( object->ComPointer );

// If the object has ancillary objects, destroy them.
if( m_Ancillary->ContainsKey( object->ComPointer ) )
{
for each( ComObject^ ancillary in m_Ancillary[object->ComPointer])
{
// By setting the owner to nullptr just before we release this object,
// we prevent an exception being thrown about the inability to release
// ancillary objects (since this delete call causes us to go through
// the same machinery that users use to Dispose() objects, and they should
// not be allowed to do that to ancillary objects).
ancillary->Owner = nullptr;
delete ancillary;
}

m_Ancillary[object->ComPointer]->Clear();
}

ObjectRemoved( nullptr, gcnew ObjectTableEventArgs( object ) );
}
finally
{
Monitor::Exit( m_SyncObject );
}
return true;
}

String^ ObjectTable::ReportLeaks()
{
StringBuilder^ output = gcnew StringBuilder();

Monitor::Enter( m_SyncObject );
try
{
for each( KeyValuePair<IntPtr, ComObject^> pair in m_Table )
{
output->AppendFormat( CultureInfo::InvariantCulture, "Object of type {0} was not disposed. Stack trace of object creation:\n", pair.Value->GetType() );

if( pair.Value->CreationSource == nullptr )
continue;

for each( StackFrame^ frame in pair.Value->CreationSource->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->AppendFormat( CultureInfo::InvariantCulture, "\t{0}({1},{2}): {3}\n",
frame->GetFileName(),
frame->GetFileLineNumber(),
frame->GetFileColumnNumber(),
frame->GetMethod() );
}
}

output->AppendFormat( CultureInfo::InvariantCulture, "Total of {0} objects still alive.\n", m_Table->Count );
}
finally
{
Monitor::Exit( m_SyncObject );
}
return output->ToString();
}

ReadOnlyCollection<ComObject^>^ ObjectTable::Objects::get()
{
return gcnew ReadOnlyCollection<ComObject^>( gcnew List<ComObject^>( m_Table->Values ) );
}

Object^ ObjectTable::SyncObject::get()
{
return m_SyncObject;
}
}

Change log

r2181 by mike.popoloski on Mar 12, 2012   Diff
Fixed leak reporter to save memory by
using StringBuilder. Resolves  issue 858 .
Go to: 

Older revisions

r2166 by mike.popoloski on Jan 28, 2012   Diff
Updated copyright dates.
r1974 by mike.popoloski on Feb 28, 2011   Diff
Fixed DXGI constructors that act like
QueryInterface to not fail when object
already exists in the object table.
Resolves  issue 764 .
r1863 by josh.petrie on Jan 7, 2011   Diff
Copyright update.
All revisions of this file

File info

Size: 6284 bytes, 218 lines
Powered by Google Project Hosting