Skip to content

Commit 691c5c0

Browse files
lostmsuMartin-Molinero
authored andcommitted
fixed leak in NewReference.Move
fixes pythonnet#1872
1 parent e872e36 commit 691c5c0

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

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
}

tests/test_constructors.py

Lines changed: 30 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

@@ -69,3 +70,32 @@ def test_default_constructor_fallback():
6970

7071
with pytest.raises(TypeError):
7172
ob = DefaultConstructorMatching("2")
73+
74+
def test_constructor_leak():
75+
from System import Uri
76+
from Python.Runtime import Runtime
77+
78+
uri = Uri("http://www.python.org")
79+
Runtime.TryCollectingGarbage(20)
80+
ref_count = sys.getrefcount(uri)
81+
82+
# check disabled due to GC uncertainty
83+
# assert ref_count == 1
84+
85+
86+
87+
def test_string_constructor():
88+
from System import String, Char, Array
89+
90+
ob = String('A', 10)
91+
assert ob == 'A' * 10
92+
93+
arr = Array[Char](10)
94+
for i in range(10):
95+
arr[i] = Char(str(i))
96+
97+
ob = String(arr)
98+
assert ob == "0123456789"
99+
100+
ob = String(arr, 5, 4)
101+
assert ob == "5678"

0 commit comments

Comments
 (0)