8000 Delete target object from event handler collections when it has no more event handlers by lostmsu · Pull Request #1973 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Delete target object from event handler collections when it has no more event handlers #1973

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
delete target object from event handler collections when it has no mo…
…re event handlers

fixes #1972
  • Loading branch information
lostmsu committed Oct 13, 2022
commit 5a28fd41db0bd1a78477717dbb91f1a10703a074
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ This document follows the conventions laid out in [Keep a CHANGELOG][].

## 8000 # Fixed

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


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

Expand Down
67 changes: 67 additions & 0 deletions src/embed_tests/Events.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using System;
using System.Diagnostics;
using System.Threading;

using NUnit.Framework;

using Python.Runtime;

namespace Python.EmbeddingTest;

public class Events
{
[OneTimeSetUp]
public void SetUp()
{
PythonEngine.Initialize();
}

[OneTimeTearDown]
public void Dispose()
{
PythonEngine.Shutdown();
}

[Test]
public void UsingDoesNotLeak()
{
using var scope = Py.CreateScope();
scope.Exec(@"
import gc

from Python.EmbeddingTest import ClassWithEventHandler

def event_handler():
pass

for _ in range(2000):
example = ClassWithEventHandler()
example.LeakEvent += event_handler
example.LeakEvent -= event_handler
del example

gc.collect()
");
Runtime.Runtime.TryCollectingGarbage(10);
Assert.AreEqual(0, ClassWithEventHandler.alive);
}
}

public class ClassWithEventHandler
{
internal static int alive;

public event EventHandler LeakEvent;
private Array arr; // dummy array to exacerbate memory leak

public ClassWithEventHandler()
{
Interlocked.Increment(ref alive);
this.arr = new int[800];
}

~ClassWithEventHandler()
{
Interlocked.Decrement(ref alive);
}
}
4 changes: 4 additions & 0 deletions src/runtime/Util/EventHandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ internal bool RemoveEventHandler(BorrowedReference target, BorrowedReference han
continue;
}
list.RemoveAt(i);
if (list.Count == 0)
{
Remove(key);
}
return true;
}

Expand Down
0