Skip to content

Commit 9bb8fe6

Browse files
authored
Merge pull request #1873 from losttech/bugs/ctor-leak
Fixed a leak in `NewReference.Move`
2 parents 863397a + 469ec67 commit 9bb8fe6

File tree

4 files changed

+17
-1
lines changed

4 files changed

+17
-1
lines changed

src/runtime/Finalizer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ internal IncorrectRefCountException(IntPtr ptr)
106106

107107
#endregion
108108

109+
[ForbidPythonThreads]
109110
public void Collect() => this.DisposeAll();
110111

111112
internal void ThrottledCollect()

src/runtime/Native/NewReference.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public PyObject MoveToPyObject()
4747
/// </summary>
4848
public NewReference Move()
4949
{
50-
var result = new NewReference(this);
50+
var result = DangerousFromPointer(this.DangerousGetAddress());
5151
this.pointer = default;
5252
return result;
5353
}

src/runtime/Runtime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
359359
/// </summary>
360360
/// <param name="runs">Total number of GC loops to run</param>
361361
/// <returns><c>true</c> if a steady state was reached upon the requested number of tries (e.g. on the last try no objects were collected).</returns>
362+
[ForbidPythonThreads]
362363
public static bool TryCollectingGarbage(int runs)
363364
=> TryCollectingGarbage(runs, forceBreakLoops: false);
364365

tests/test_constructors.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""Test CLR class constructor support."""
44

55
import pytest
6+
import sys
67

78
import System
89

@@ -71,6 +72,19 @@ def test_default_constructor_fallback():
7172
ob = DefaultConstructorMatching("2")
7273

7374

75+
def test_constructor_leak():
76+
from System import Uri
77+
from Python.Runtime import Runtime
78+
79+
uri = Uri("http://www.python.org")
80+
Runtime.TryCollectingGarbage(20)
81+
ref_count = sys.getrefcount(uri)
82+
83+
# check disabled due to GC uncertainty
84+
# assert ref_count == 1
85+
86+
87+
7488
def test_string_constructor():
7589
from System import String, Char, Array
7690

0 commit comments

Comments
 (0)