Skip to content

Commit 51b2b9c

Browse files
committed
Update ComPointer
1 parent fbcc077 commit 51b2b9c

File tree

1 file changed

+35
-15
lines changed
  • Rubberduck.VBEEditor/ComManagement/TypeLibs/Unmanaged

1 file changed

+35
-15
lines changed

Rubberduck.VBEEditor/ComManagement/TypeLibs/Unmanaged/ComPointer.cs

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -107,29 +107,41 @@ private ComPointer(IntPtr pUnk, bool addRef)
107107

108108
Interface = (TComInterface)RdMarshal.GetTypedObjectForIUnknown(pUnk, typeof(TComInterface));
109109

110-
#if DEBUG && TRACE_COM_POINTERS
110+
ConstructorPointerPrint(refCount);
111+
}
112+
113+
[Conditional("TRACE_COM_POINTERS")]
114+
private void ConstructorPointerPrint(int refCount)
115+
{
111116
Debug.Print($"ComPointer:: Created from pointer: pUnk: {RdMarshal.FormatPtr(_pUnk)} interface: {typeof(TComInterface).Name} - {Interface.GetHashCode()} addRef: {_addRef} refCount: {refCount}");
112-
#endif
113117
}
114118

115119
private ComPointer(IntPtr pUnk, TComInterface comInterface)
116120
{
117121
_pUnk = pUnk;
118122
Interface = comInterface;
119123

120-
#if DEBUG && TRACE_COM_POINTERS
124+
ConstructorAggregatedPrint();
125+
}
126+
127+
[Conditional("TRACE_COM_POINTERS")]
128+
private void ConstructorAggregatedPrint()
129+
{
121130
Debug.Print($"ComPointer:: Created from aggregation: pUnk: {RdMarshal.FormatPtr(_pUnk)} interface: {typeof(TComInterface).Name} - {Interface.GetHashCode()} addRef: {_addRef}");
122-
#endif
123131
}
124132

125133
private ComPointer(TComInterface comInterface)
126134
{
127135
Interface = comInterface;
128136
_pUnk = RdMarshal.GetIUnknownForObject(Interface);
129137

130-
#if DEBUG && TRACE_COM_POINTERS
138+
ConstructorObjectPrint();
139+
}
140+
141+
[Conditional("TRACE_COM_POINTERS")]
142+
private void ConstructorObjectPrint()
143+
{
131144
Debug.Print($"ComPointer:: Created from object: pUnk: {RdMarshal.FormatPtr(_pUnk)} interface: {typeof(TComInterface).Name} - {Interface.GetHashCode()} addRef: {_addRef}");
132-
#endif
133145
}
134146

135147
internal IntPtr ExtractPointer()
@@ -145,9 +157,22 @@ private void ReleaseUnmanagedResources()
145157
if(_disposed) return;
146158

147159
var rcwCount = RdMarshal.ReleaseComObject(Interface);
160+
var addRef = _addRef;
148161

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)
151176
{
152177
// Temporarily add a ref so that we can safely call IUnknown::Release
153178
// to report the ref count in the log.
@@ -156,13 +181,8 @@ private void ReleaseUnmanagedResources()
156181
var refCount = RdMarshal.Release(_pUnk);
157182

158183
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;
166186
}
167187

168188
public void Dispose()

0 commit comments

Comments
 (0)