Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit5a28fd4

Browse files
committed
delete target object from event handler collections when it has no more event handlers
fixes#1972
1 parent6838ee1 commit5a28fd4

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

‎CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].
1313

1414
###Fixed
1515

16+
- Fixed objects leaking when Python attached event handlers to them even if they were later removed
17+
1618

1719
##[3.0.0](https://github.com/pythonnet/pythonnet/releases/tag/v3.0.0) - 2022-09-29
1820

‎src/embed_tests/Events.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
usingSystem;
2+
usingSystem.Diagnostics;
3+
usingSystem.Threading;
4+
5+
usingNUnit.Framework;
6+
7+
usingPython.Runtime;
8+
9+
namespacePython.EmbeddingTest;
10+
11+
publicclassEvents
12+
{
13+
[OneTimeSetUp]
14+
publicvoidSetUp()
15+
{
16+
PythonEngine.Initialize();
17+
}
18+
19+
[OneTimeTearDown]
20+
publicvoidDispose()
21+
{
22+
PythonEngine.Shutdown();
23+
}
24+
25+
[Test]
26+
publicvoidUsingDoesNotLeak()
27+
{
28+
usingvarscope=Py.CreateScope();
29+
scope.Exec(@"
30+
import gc
31+
32+
from Python.EmbeddingTest import ClassWithEventHandler
33+
34+
def event_handler():
35+
pass
36+
37+
for _ in range(2000):
38+
example = ClassWithEventHandler()
39+
example.LeakEvent += event_handler
40+
example.LeakEvent -= event_handler
41+
del example
42+
43+
gc.collect()
44+
");
45+
Runtime.Runtime.TryCollectingGarbage(10);
46+
Assert.AreEqual(0,ClassWithEventHandler.alive);
47+
}
48+
}
49+
50+
publicclassClassWithEventHandler
51+
{
52+
internalstaticintalive;
53+
54+
publiceventEventHandlerLeakEvent;
55+
privateArrayarr;// dummy array to exacerbate memory leak
56+
57+
publicClassWithEventHandler()
58+
{
59+
Interlocked.Increment(refalive);
60+
this.arr=newint[800];
61+
}
62+
63+
~ClassWithEventHandler()
64+
{
65+
Interlocked.Decrement(refalive);
66+
}
67+
}

‎src/runtime/Util/EventHandlerCollection.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ internal bool RemoveEventHandler(BorrowedReference target, BorrowedReference han
9999
continue;
100100
}
101101
list.RemoveAt(i);
102+
if(list.Count==0)
103+
{
104+
Remove(key);
105+
}
102106
returntrue;
103107
}
104108

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp