Skip to content

Commit 83f88d2

Browse files
Merge pull request #70 from Martin-Molinero/bug-memory-leak
Fix memory leak
2 parents 27b5b7d + fb3a6a3 commit 83f88d2

File tree

7 files changed

+38
-6
lines changed

7 files changed

+38
-6
lines changed

src/perf_tests/Python.PerformanceTests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
1414
</PackageReference>
1515
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.*" />
16-
<PackageReference Include="quantconnect.pythonnet" Version="2.0.16" GeneratePathProperty="true">
16+
<PackageReference Include="quantconnect.pythonnet" Version="2.0.17" GeneratePathProperty="true">
1717
<IncludeAssets>compile</IncludeAssets>
1818
</PackageReference>
1919
</ItemGroup>
@@ -25,7 +25,7 @@
2525
</Target>
2626

2727
<Target Name="CopyBaseline" AfterTargets="Build">
28-
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.16\lib\net5.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
28+
<Copy SourceFiles="$(NuGetPackageRoot)quantconnect.pythonnet\2.0.17\lib\net5.0\Python.Runtime.dll" DestinationFolder="$(OutDir)baseline" />
2929
</Target>
3030

3131
<Target Name="CopyNewBuild" AfterTargets="Build">

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/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44
[assembly: InternalsVisibleTo("Python.EmbeddingTest, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
55
[assembly: InternalsVisibleTo("Python.Test, PublicKey=00240000048000009400000006020000002400005253413100040000110000005ffd8f49fb44ab0641b3fd8d55e749f716e6dd901032295db641eb98ee46063cbe0d4a1d121ef0bc2af95f8a7438d7a80a3531316e6b75c2dae92fb05a99f03bf7e0c03980e1c3cfb74ba690aca2f3339ef329313bcc5dccced125a4ffdc4531dcef914602cd5878dc5fbb4d4c73ddfbc133f840231343e013762884d6143189")]
66

7-
[assembly: AssemblyVersion("2.0.16")]
8-
[assembly: AssemblyFileVersion("2.0.16")]
7+
[assembly: AssemblyVersion("2.0.17")]
8+
[assembly: AssemblyFileVersion("2.0.17")]

src/runtime/Python.Runtime.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<RootNamespace>Python.Runtime</RootNamespace>
66
<AssemblyName>Python.Runtime</AssemblyName>
77
<PackageId>QuantConnect.pythonnet</PackageId>
8-
<Version>2.0.16</Version>
8+
<Version>2.0.17</Version>
99
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
1010
<PackageLicenseFile>LICENSE</PackageLicenseFile>
1111
<RepositoryUrl>https://github.com/pythonnet/pythonnet</RepositoryUrl>

src/runtime/Runtime.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,7 @@ static bool TryCollectingGarbage(int runs, bool forceBreakLoops)
362362
/// </summary>
363363
/// <param name="runs">Total number of GC loops to run</param>
364364
/// <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>
365+
[ForbidPythonThreads]
365366
public static bool TryCollectingGarbage(int runs)
366367
=> TryCollectingGarbage(runs, forceBreakLoops: false);
367368

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)