jeikabu / jeikabu/nng.NETCore

AsyncBase implementations not freeing allocated nng_ctx

Open
#110 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
45
Forks
25
PR merge metrics
No merged PRs in 30d

Description

Some classes that extend AsyncBase contain a member field Ctx. The field implements INngCtx which does not implement IDisposable but the actual implementation class NngCtx does. This leads to the implementation never disposing of the Ctx field and leaking memory when the async context is disposed of.

You can reproduce it by running this code. If you run it in Visual Studio using heap profiling, you can see that every 100 ms there will be an allocation originating from nng.dll:

```csharp
using nng;
var factory = nng.NngLoadContext.Init(new(Path.GetDirectoryName(typeof(Program).Assembly.Location)));
using var repSocket = factory.ReplierOpen().ThenListen("inproc://1").Unwrap();
while (true)
{
using var repAsyncCtx = repSocket.CreateAsyncContext(factory).Unwrap();
await Task.Delay(100);
}
```

![image](https://user-images.githubusercontent.com/36773277/233305027-182ebf1a-cb38-436d-a370-9aee17179f91.png)

Adding this line to the while loop makes the allocations dissappear (i.e. memory is freed):
```csharp
((IDisposable)repAsyncCtx.Ctx).Dispose();
```

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.