UDPCommunication.cs produces memory leak when compiling with IL2CPP backend
agardony opened this issue · 1 comments
I've been trying to build a hololens project using the the il2cpp backend instead of .net. The il2cpp backend gives access to additional namespaces that are not included in the .net backend. The il2cpp backend converts the c# project to c++ and will become the preferred backend in the next couple years.
Everything builds fine but when I deploy the app I see a crippling memory leak (~20mb increase per second) that crashes the app after about 30 seconds. After a lot of testing I've been able to trace the leak to happening when gaze events are subscribed to. Specifically the leak occurs in the UDPCommunication.cs script in the Socket_MessageReceived function. For reasons unknown and suggested by this discussion thread the line:
Stream streamIn = args.GetDataStream().AsStreamForRead();
seems to cause the problem. I believe the memory allocated in this line is not properly garbage collected leading to the leak.
Rewriting the offending section to not use Stream (see code below) appears to correct the memory leak for IL2CPP-based builds.
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender, Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
using (var reader = args.GetDataReader())
{
var buf = new byte[reader.UnconsumedBufferLength];
reader.ReadBytes(buf);
if (ExecuteOnMainThread.Count == 0)
{
ExecuteOnMainThread.Enqueue(() => {InterpreteUDPData(buf);});
}
}
}
Closing this issue, as it relates to the legacy version pre v1.0.