3 * Copyright (c) 2007-2010 SlimDX Group
\r
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
\r
6 * of this software and associated documentation files (the "Software"), to deal
\r
7 * in the Software without restriction, including without limitation the rights
\r
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
\r
9 * copies of the Software, and to permit persons to whom the Software is
\r
10 * furnished to do so, subject to the following conditions:
\r
12 * The above copyright notice and this permission notice shall be included in
\r
13 * all copies or substantial portions of the Software.
\r
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
\r
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
\r
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
\r
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
\r
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
\r
24 #include "ObjectTable.h"
\r
25 #include "InternalHelpers.h"
\r
26 #include "ComObject.h"
\r
28 using namespace System;
\r
29 using namespace System::Threading;
\r
30 using namespace System::Globalization;
\r
31 using namespace System::Collections::ObjectModel;
\r
32 using namespace System::Collections::Generic;
\r
33 using namespace System::Diagnostics;
\r
37 static ObjectTable::ObjectTable()
\r
39 m_Table = gcnew Dictionary<IntPtr, ComObject^>();
\r
40 m_Ancillary = gcnew Dictionary<IntPtr, List<ComObject^>^>();
\r
41 m_SyncObject = gcnew Object();
\r
43 AppDomain::CurrentDomain->DomainUnload += gcnew System::EventHandler( OnExit );
\r
44 AppDomain::CurrentDomain->ProcessExit += gcnew System::EventHandler( OnExit );
\r
47 ObjectTable::ObjectTable()
\r
51 void ObjectTable::OnExit( Object^ sender, EventArgs^ e )
\r
53 SLIMDX_UNREFERENCED_PARAMETER(sender);
\r
54 SLIMDX_UNREFERENCED_PARAMETER(e);
\r
56 String^ leakString = ReportLeaks();
\r
57 Debug::Write( leakString );
\r
60 ComObject^ ObjectTable::Find( IntPtr nativeObject )
\r
62 Monitor::Enter( m_SyncObject );
\r
65 if( m_Table->ContainsKey( nativeObject ) )
\r
67 return m_Table[nativeObject];
\r
74 Monitor::Exit( m_SyncObject );
\r
78 bool ObjectTable::Contains( ComObject^ object )
\r
80 Monitor::Enter( m_SyncObject );
\r
83 return m_Table->ContainsKey( object->ComPointer );
\r
87 Monitor::Exit( m_SyncObject );
\r
91 void ObjectTable::Add( ComObject^ object, ComObject^ owner )
\r
93 if( object == nullptr )
\r
94 throw gcnew ArgumentNullException( "comObject" );
\r
96 // Record tracking information
\r
97 object->SetCreationTime( static_cast<int>( Configuration::Timer->ElapsedMilliseconds ) );
\r
98 if( Configuration::EnableObjectTracking )
\r
99 object->SetSource( gcnew StackTrace( 2, true ) );
\r
101 // Add to the table
\r
102 Monitor::Enter( m_SyncObject );
\r
105 m_Table->Add( object->ComPointer, object );
\r
107 if( owner != nullptr )
\r
109 if( !m_Ancillary->ContainsKey( owner->ComPointer ) )
\r
111 m_Ancillary->Add( owner->ComPointer, gcnew List<ComObject^>() );
\r
113 m_Ancillary[owner->ComPointer]->Add( object );
\r
116 ObjectAdded( nullptr, gcnew ObjectTableEventArgs( object ) );
\r
120 Monitor::Exit( m_SyncObject );
\r
124 bool ObjectTable::Remove( ComObject^ object )
\r
126 if( object == nullptr )
\r
127 throw gcnew ArgumentNullException( "comObject" );
\r
129 Monitor::Enter( m_SyncObject );
\r
132 if( !m_Table->ContainsKey( object->ComPointer ) )
\r
135 m_Table->Remove( object->ComPointer );
\r
137 // If the object has ancillary objects, destroy them.
\r
138 if( m_Ancillary->ContainsKey( object->ComPointer ) )
\r
140 for each( ComObject^ ancillary in m_Ancillary[object->ComPointer])
\r
142 // By setting the owner to nullptr just before we release this object,
\r
143 // we prevent an exception being thrown about the inability to release
\r
144 // ancillary objects (since this delete call causes us to go through
\r
145 // the same machinery that users use to Dispose() objects, and they should
\r
146 // not be allowed to do that to ancillary objects).
\r
147 ancillary->Owner = nullptr;
\r
151 m_Ancillary[object->ComPointer]->Clear();
\r
154 ObjectRemoved( nullptr, gcnew ObjectTableEventArgs( object ) );
\r
158 Monitor::Exit( m_SyncObject );
\r
163 String^ ObjectTable::ReportLeaks()
\r
165 String^ output = "";
\r
167 Monitor::Enter( m_SyncObject );
\r
170 for each( KeyValuePair<IntPtr, ComObject^> pair in m_Table )
\r
172 output += String::Format( CultureInfo::InvariantCulture, "Object of type {0} was not disposed. Stack trace of object creation:\n", pair.Value->GetType() );
\r
174 if( pair.Value->CreationSource == nullptr )
\r
177 for each( StackFrame^ frame in pair.Value->CreationSource->GetFrames() )
\r
179 if( frame->GetFileLineNumber() == 0 )
\r
181 // Compiler autogenerated functions and the like can cause stack frames with no info;
\r
182 // that's the only time the line number is 0 and since it's not a useful frame to see,
\r
187 output += String::Format( CultureInfo::InvariantCulture, "\t{0}({1},{2}): {3}\n",
\r
188 frame->GetFileName(),
\r
189 frame->GetFileLineNumber(),
\r
190 frame->GetFileColumnNumber(),
\r
191 frame->GetMethod() );
\r
195 output += String::Format( CultureInfo::InvariantCulture, "Total of {0} objects still alive.\n", m_Table->Count );
\r
199 Monitor::Exit( m_SyncObject );
\r
204 ReadOnlyCollection<ComObject^>^ ObjectTable::Objects::get()
\r
206 return gcnew ReadOnlyCollection<ComObject^>( gcnew List<ComObject^>( m_Table->Values ) );
\r
209 Object^ ObjectTable::SyncObject::get()
\r
211 return m_SyncObject;
\r