|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
2 | 3 | using System.Diagnostics;
|
3 | 4 | using System.Reflection;
|
4 | 5 | using System.Runtime.InteropServices;
|
@@ -256,37 +257,81 @@ public void TestClassReference()
|
256 | 257 | [Test]
|
257 | 258 | public void CrossReleaseBuiltinType()
|
258 | 259 | {
|
| 260 | + void ExecTest() |
| 261 | + { |
| 262 | + try |
| 263 | + { |
| 264 | + var numRef = CreateNumReference(); |
| 265 | + GC.Collect(); |
| 266 | + GC.WaitForPendingFinalizers(); // <- this will put former `num` into Finalizer queue |
| 267 | + Finalizer.Instance.Collect(forceDispose: true); |
| 268 | + // ^- this will call PyObject.Dispose, which will call XDecref on `num.Handle`, |
| 269 | + // but Python interpreter from "run" 1 is long gone, so it will corrupt memory instead. |
| 270 | + Assert.False(numRef.IsAlive); |
| 271 | + } |
| 272 | + finally |
| 273 | + { |
| 274 | + PythonEngine.Shutdown(); |
| 275 | + } |
| 276 | + } |
| 277 | + |
| 278 | + var errorArgs = new List<Finalizer.ErrorArgs>(); |
| 279 | + void ErrorHandler(object sender, Finalizer.ErrorArgs e) |
| 280 | + { |
| 281 | + errorArgs.Add(e); |
| 282 | + } |
| 283 | + Finalizer.Instance.ErrorHandler += ErrorHandler; |
259 | 284 | try
|
260 | 285 | {
|
261 |
| - var numRef = CreateNumReference(); |
262 |
| - GC.Collect(); |
263 |
| - GC.WaitForPendingFinalizers(); // <- this will put former `num` into Finalizer queue |
264 |
| - Finalizer.Instance.Collect(forceDispose: true); |
265 |
| - // ^- this will call PyObject.Dispose, which will call XDecref on `num.Handle`, |
266 |
| - // but Python interpreter from "run" 1 is long gone, so it will corrupt memory instead. |
267 |
| - Assert.False(numRef.IsAlive); |
| 286 | + for (int i = 0; i < 10; i++) |
| 287 | + { |
| 288 | + ExecTest(); |
| 289 | + } |
268 | 290 | }
|
269 | 291 | finally
|
270 | 292 | {
|
271 |
| - PythonEngine.Shutdown(); |
| 293 | + Finalizer.Instance.ErrorHandler -= ErrorHandler; |
272 | 294 | }
|
| 295 | + Assert.AreEqual(errorArgs.Count, 0); |
273 | 296 | }
|
274 | 297 |
|
275 | 298 | [Test]
|
276 | 299 | public void CrossReleaseCustomType()
|
277 | 300 | {
|
| 301 | + void ExecTest() |
| 302 | + { |
| 303 | + try |
| 304 | + { |
| 305 | + var objRef = CreateConcreateObject(); |
| 306 | + GC.Collect(); |
| 307 | + GC.WaitForPendingFinalizers(); |
| 308 | + Finalizer.Instance.Collect(forceDispose: true); |
| 309 | + Assert.False(objRef.IsAlive); |
| 310 | + } |
| 311 | + finally |
| 312 | + { |
| 313 | + PythonEngine.Shutdown(); |
| 314 | + } |
| 315 | + } |
| 316 | + |
| 317 | + var errorArgs = new List<Finalizer.ErrorArgs>(); |
| 318 | + void ErrorHandler(object sender, Finalizer.ErrorArgs e) |
| 319 | + { |
| 320 | + errorArgs.Add(e); |
| 321 | + } |
| 322 | + Finalizer.Instance.ErrorHandler += ErrorHandler; |
278 | 323 | try
|
279 | 324 | {
|
280 |
| - var objRef = CreateConcreateObject(); |
281 |
| - GC.Collect(); |
282 |
| - GC.WaitForPendingFinalizers(); |
283 |
| - Finalizer.Instance.Collect(forceDispose: true); |
284 |
| - Assert.False(objRef.IsAlive); |
| 325 | + for (int i = 0; i < 10; i++) |
| 326 | + { |
| 327 | + ExecTest(); |
| 328 | + } |
285 | 329 | }
|
286 | 330 | finally
|
287 | 331 | {
|
288 |
| - PythonEngine.Shutdown(); |
| 332 | + Finalizer.Instance.ErrorHandler -= ErrorHandler; |
289 | 333 | }
|
| 334 | + Assert.AreEqual(errorArgs.Count, 0); |
290 | 335 | }
|
291 | 336 |
|
292 | 337 | private static WeakReference CreateNumReference()
|
|
0 commit comments