OSDN Git Service

DTXMania089リリースに際してのtag付け。
[dtxmania/dtxmania.git] / 110401(DTXMania089) / SlimDXc_Jun2010(VC++2008) / source / ObjectTable.cpp
1 #include "stdafx.h"\r
2 /*\r
3 * Copyright (c) 2007-2010 SlimDX Group\r
4\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
11\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
14\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
21 * THE SOFTWARE.\r
22 */\r
23 \r
24 #include "ObjectTable.h"\r
25 #include "InternalHelpers.h"\r
26 #include "ComObject.h"\r
27 \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
34 \r
35 namespace SlimDX\r
36 {\r
37         static ObjectTable::ObjectTable()\r
38         {\r
39                 m_Table = gcnew Dictionary<IntPtr, ComObject^>();\r
40                 m_Ancillary = gcnew Dictionary<IntPtr, List<ComObject^>^>();\r
41                 m_SyncObject = gcnew Object();\r
42 \r
43                 AppDomain::CurrentDomain->DomainUnload += gcnew System::EventHandler( OnExit );\r
44                 AppDomain::CurrentDomain->ProcessExit += gcnew System::EventHandler( OnExit );\r
45         }\r
46 \r
47         ObjectTable::ObjectTable()\r
48         {\r
49         }\r
50 \r
51         void ObjectTable::OnExit( Object^ sender, EventArgs^ e )\r
52         {\r
53                 SLIMDX_UNREFERENCED_PARAMETER(sender);\r
54                 SLIMDX_UNREFERENCED_PARAMETER(e);\r
55 \r
56                 String^ leakString = ReportLeaks();\r
57                 Debug::Write( leakString );\r
58         }\r
59 \r
60         ComObject^ ObjectTable::Find( IntPtr nativeObject )\r
61         {\r
62                 Monitor::Enter( m_SyncObject );\r
63                 try\r
64                 {\r
65                         if( m_Table->ContainsKey( nativeObject ) )\r
66                         {\r
67                                 return m_Table[nativeObject];\r
68                         }\r
69 \r
70                         return nullptr;\r
71                 }\r
72                 finally\r
73                 {\r
74                         Monitor::Exit( m_SyncObject );\r
75                 }\r
76         }\r
77 \r
78         bool ObjectTable::Contains( ComObject^ object )\r
79         {\r
80                 Monitor::Enter( m_SyncObject );\r
81                 try\r
82                 {\r
83                         return m_Table->ContainsKey( object->ComPointer );\r
84                 }\r
85                 finally\r
86                 {\r
87                         Monitor::Exit( m_SyncObject );\r
88                 }\r
89         }\r
90 \r
91         void ObjectTable::Add( ComObject^ object, ComObject^ owner )\r
92         {\r
93                 if( object == nullptr )\r
94                         throw gcnew ArgumentNullException( "comObject" );\r
95 \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
100 \r
101                 // Add to the table\r
102                 Monitor::Enter( m_SyncObject );\r
103                 try\r
104                 {\r
105                         m_Table->Add( object->ComPointer, object );\r
106                         \r
107                         if( owner != nullptr ) \r
108                         {\r
109                                 if( !m_Ancillary->ContainsKey( owner->ComPointer ) )\r
110                                 {\r
111                                         m_Ancillary->Add( owner->ComPointer, gcnew List<ComObject^>() );\r
112                                 }\r
113                                 m_Ancillary[owner->ComPointer]->Add( object );\r
114                         }\r
115                         \r
116                         ObjectAdded( nullptr, gcnew ObjectTableEventArgs( object ) );\r
117                 }\r
118                 finally\r
119                 {\r
120                         Monitor::Exit( m_SyncObject );\r
121                 }\r
122         }\r
123 \r
124         bool ObjectTable::Remove( ComObject^ object )\r
125         {\r
126                 if( object == nullptr )\r
127                         throw gcnew ArgumentNullException( "comObject" );\r
128 \r
129                 Monitor::Enter( m_SyncObject );\r
130                 try\r
131                 {\r
132                         if( !m_Table->ContainsKey( object->ComPointer ) )\r
133                                 return false;\r
134 \r
135                         m_Table->Remove( object->ComPointer );\r
136                 \r
137                         // If the object has ancillary objects, destroy them.\r
138                         if( m_Ancillary->ContainsKey( object->ComPointer ) )\r
139                         {\r
140                                 for each( ComObject^ ancillary in m_Ancillary[object->ComPointer])\r
141                                 {\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
148                                         delete ancillary;\r
149                                 }\r
150                                 \r
151                                 m_Ancillary[object->ComPointer]->Clear();\r
152                         }\r
153                                                 \r
154                         ObjectRemoved( nullptr, gcnew ObjectTableEventArgs( object ) );\r
155                 }\r
156                 finally\r
157                 {\r
158                         Monitor::Exit( m_SyncObject );\r
159                 }\r
160                 return true;\r
161         }\r
162 \r
163         String^ ObjectTable::ReportLeaks()\r
164         {\r
165                 String^ output = "";\r
166 \r
167                 Monitor::Enter( m_SyncObject );\r
168                 try\r
169                 {\r
170                         for each( KeyValuePair<IntPtr, ComObject^> pair in m_Table )\r
171                         {\r
172                                 output += String::Format( CultureInfo::InvariantCulture, "Object of type {0} was not disposed. Stack trace of object creation:\n", pair.Value->GetType() );\r
173 \r
174                                 if( pair.Value->CreationSource == nullptr )\r
175                                         continue;\r
176 \r
177                                 for each( StackFrame^ frame in pair.Value->CreationSource->GetFrames() )\r
178                                 {\r
179                                         if( frame->GetFileLineNumber() == 0 )\r
180                                         {\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
183                                                 // we'll skip it\r
184                                                 continue;\r
185                                         }\r
186 \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
192                                 }\r
193                         }\r
194 \r
195                         output += String::Format( CultureInfo::InvariantCulture, "Total of {0} objects still alive.\n", m_Table->Count );\r
196                 }\r
197                 finally\r
198                 {\r
199                         Monitor::Exit( m_SyncObject );\r
200                 }\r
201                 return output;\r
202         }\r
203 \r
204         ReadOnlyCollection<ComObject^>^ ObjectTable::Objects::get()\r
205         {\r
206                 return gcnew ReadOnlyCollection<ComObject^>( gcnew List<ComObject^>( m_Table->Values ) );\r
207         }\r
208 \r
209         Object^ ObjectTable::SyncObject::get()\r
210         {\r
211                 return m_SyncObject;\r
212         }\r
213 }