File tree 3 files changed +30
-6
lines changed
libraries/System.Private.CoreLib/src/System
tests/Interop/COM/ComWrappers/WeakReference
3 files changed +30
-6
lines changed Original file line number Diff line number Diff line change @@ -110,6 +110,10 @@ public void SetTarget(T target)
110
110
if ( ( th & ComAwareBit ) != 0 || comInfo != null )
111
111
{
112
112
ComAwareWeakReference . SetTarget ( ref _taggedHandle , target , comInfo ) ;
113
+
114
+ // must keep the instance alive as long as we use the handle.
115
+ GC . KeepAlive ( this ) ;
116
+
113
117
return ;
114
118
}
115
119
#endif
@@ -133,13 +137,22 @@ private T? Target
133
137
if ( th == 0 )
134
138
return default ;
135
139
140
+ T ? target ;
141
+
136
142
#if FEATURE_COMINTEROP || FEATURE_COMWRAPPERS
137
143
if ( ( th & ComAwareBit ) != 0 )
138
- return Unsafe . As < T ? > ( ComAwareWeakReference . GetTarget ( th ) ) ;
144
+ {
145
+ target = Unsafe . As < T ? > ( ComAwareWeakReference . GetTarget ( th ) ) ;
146
+
147
+ // must keep the instance alive as long as we use the handle.
148
+ GC . KeepAlive ( this ) ;
149
+
150
+ return target ;
151
+ }
139
152
#endif
140
153
141
154
// unsafe cast is ok as the handle cannot be destroyed and recycled while we keep the instance alive
142
- T ? target = Unsafe . As < T ? > ( GCHandle . InternalGet ( th ) ) ;
155
+ target = Unsafe . As < T ? > ( GCHandle . InternalGet ( th ) ) ;
143
156
144
157
// must keep the instance alive as long as we use the handle.
145
158
GC . KeepAlive ( this ) ;
Original file line number Diff line number Diff line change @@ -157,13 +157,22 @@ public virtual object? Target
157
157
if ( th == 0 )
158
158
return default ;
159
159
160
+ object ? target ;
161
+
160
162
#if FEATURE_COMINTEROP || FEATURE_COMWRAPPERS
161
163
if ( ( th & ComAwareBit ) != 0 )
162
- return ComAwareWeakReference . GetTarget ( th ) ;
164
+ {
165
+ target = ComAwareWeakReference . GetTarget ( th ) ;
166
+
167
+ // must keep the instance alive as long as we use the handle.
168
+ GC . KeepAlive ( this ) ;
169
+
170
+ return target ;
171
+ }
163
172
#endif
164
173
165
174
// unsafe cast is ok as the handle cannot be destroyed and recycled while we keep the instance alive
166
- object ? target = GCHandle . InternalGet ( th ) ;
175
+ target = GCHandle . InternalGet ( th ) ;
167
176
168
177
// must keep the instance alive as long as we use the handle.
169
178
GC . KeepAlive ( this ) ;
@@ -186,6 +195,10 @@ public virtual object? Target
186
195
if ( ( th & ComAwareBit ) != 0 || comInfo != null )
187
196
{
188
197
ComAwareWeakReference . SetTarget ( ref _taggedHandle , value , comInfo ) ;
198
+
199
+ // must keep the instance alive as long as we use the handle.
200
+ GC . KeepAlive ( this ) ;
201
+
189
202
return ;
190
203
}
191
204
#endif
Original file line number Diff line number Diff line change 4
4
<!-- Registers global instances of ComWrappers -->
5
5
<UnloadabilityIncompatible >true</UnloadabilityIncompatible >
6
6
<AllowUnsafeBlocks >true</AllowUnsafeBlocks >
7
- <!-- Temporarily disabled due to https://github.com/dotnet/runtime/issues/81362 -->
8
- <GCStressIncompatible >true</GCStressIncompatible >
9
7
</PropertyGroup >
10
8
<ItemGroup >
11
9
<Compile Include =" WeakReferenceTest.cs" />
You can’t perform that action at this time.
0 commit comments