@@ -107,29 +107,41 @@ private ComPointer(IntPtr pUnk, bool addRef)
107
107
108
108
Interface = ( TComInterface ) RdMarshal . GetTypedObjectForIUnknown ( pUnk , typeof ( TComInterface ) ) ;
109
109
110
- #if DEBUG && TRACE_COM_POINTERS
110
+ ConstructorPointerPrint ( refCount ) ;
111
+ }
112
+
113
+ [ Conditional ( "TRACE_COM_POINTERS" ) ]
114
+ private void ConstructorPointerPrint ( int refCount )
115
+ {
111
116
Debug . Print ( $ "ComPointer:: Created from pointer: pUnk: { RdMarshal . FormatPtr ( _pUnk ) } interface: { typeof ( TComInterface ) . Name } - { Interface . GetHashCode ( ) } addRef: { _addRef } refCount: { refCount } ") ;
112
- #endif
113
117
}
114
118
115
119
private ComPointer ( IntPtr pUnk , TComInterface comInterface )
116
120
{
117
121
_pUnk = pUnk ;
118
122
Interface = comInterface ;
119
123
120
- #if DEBUG && TRACE_COM_POINTERS
124
+ ConstructorAggregatedPrint ( ) ;
125
+ }
126
+
127
+ [ Conditional ( "TRACE_COM_POINTERS" ) ]
128
+ private void ConstructorAggregatedPrint ( )
129
+ {
121
130
Debug . Print ( $ "ComPointer:: Created from aggregation: pUnk: { RdMarshal . FormatPtr ( _pUnk ) } interface: { typeof ( TComInterface ) . Name } - { Interface . GetHashCode ( ) } addRef: { _addRef } ") ;
122
- #endif
123
131
}
124
132
125
133
private ComPointer ( TComInterface comInterface )
126
134
{
127
135
Interface = comInterface ;
128
136
_pUnk = RdMarshal . GetIUnknownForObject ( Interface ) ;
129
137
130
- #if DEBUG && TRACE_COM_POINTERS
138
+ ConstructorObjectPrint ( ) ;
139
+ }
140
+
141
+ [ Conditional ( "TRACE_COM_POINTERS" ) ]
142
+ private void ConstructorObjectPrint ( )
143
+ {
131
144
Debug . Print ( $ "ComPointer:: Created from object: pUnk: { RdMarshal . FormatPtr ( _pUnk ) } interface: { typeof ( TComInterface ) . Name } - { Interface . GetHashCode ( ) } addRef: { _addRef } ") ;
132
- #endif
133
145
}
134
146
135
147
internal IntPtr ExtractPointer ( )
@@ -145,9 +157,22 @@ private void ReleaseUnmanagedResources()
145
157
if ( _disposed ) return ;
146
158
147
159
var rcwCount = RdMarshal . ReleaseComObject ( Interface ) ;
160
+ var addRef = _addRef ;
148
161
149
- #if DEBUG && TRACE_COM_POINTERS
150
- if ( ! _addRef )
162
+ TraceRelease ( rcwCount , ref addRef ) ;
163
+ if ( addRef )
164
+ {
165
+ RdMarshal . Release ( _pUnk ) ;
166
+ }
167
+
168
+ _disposed = true ;
169
+ }
170
+
171
+ [ Conditional ( "DEBUG" ) ]
172
+ [ Conditional ( "TRACE_COM_POINTERS" ) ]
173
+ private void TraceRelease ( int rcwCount , ref bool addRef )
174
+ {
175
+ if ( ! addRef )
151
176
{
152
177
// Temporarily add a ref so that we can safely call IUnknown::Release
153
178
// to report the ref count in the log.
@@ -156,13 +181,8 @@ private void ReleaseUnmanagedResources()
156
181
var refCount = RdMarshal . Release ( _pUnk ) ;
157
182
158
183
Debug . Print ( $ "ComPointer:: Disposed: _pUnk: { RdMarshal . FormatPtr ( _pUnk ) } _interface: { typeof ( TComInterface ) . Name } - { Interface . GetHashCode ( ) } addRef: { _addRef } rcwCount: { rcwCount } refCount: { refCount } ") ;
159
- #else
160
- if ( _addRef )
161
- {
162
- RdMarshal . Release ( _pUnk ) ;
163
- }
164
- #endif
165
- _disposed = true ;
184
+
185
+ addRef = false ;
166
186
}
167
187
168
188
public void Dispose ( )
0 commit comments