I have a client-server app. HoloLens 2 is my client and communicates with the server using UDP. My MRTK profiler shows me the app consumes memory incrementally and after 2-3 mins app crashes. I send some packets from the server to HoloLens. I tried to call GC but it doesn’t work either. I tried to remove using blocks and threads but the result didn’t change. I use only one port. Should I connect, get packets and dispose of the socket for each packet? (this is so terrible solution) Anyway, any idea how can I fix the leak?
private void Socket_MessageReceived(Windows.Networking.Sockets.DatagramSocket sender,
Windows.Networking.Sockets.DatagramSocketMessageReceivedEventArgs args)
{
//Read the message that was received from the UDP client.
Stream streamIn = args.GetDataStream().AsStreamForRead();
MemoryStream ms = Task.Run(() => ToMemoryStream(streamIn)).Result;
byte[] msgData = ms.ToArray();
ThreadManager.ExecuteOnMainThread(() =>
{
Task.Run(() => HandleData(msgData));
});
}
private async Task<MemoryStream> ToMemoryStream(Stream input)
{
try
{
using (MemoryStream ms = new MemoryStream())
{
// Read and write in blocks of 4K.
byte[] block = new byte[0x1000];
while (true)
{
int bytesRead = input.Read(block, 0, block.Length);
if (bytesRead == 0)
{
return ms;
}
ms.Write(block, 0, bytesRead);
}
}
}
finally { }
}
private async System.Threading.Tasks.Task HandleData(byte[] data)
{
using (Packet packet = new Packet(data))
{
int packetLength = packet.ReadInt();
data = packet.ReadBytes(packetLength);
}
ThreadManager.ExecuteOnMainThread(() =>
{
using (Packet packet = new Packet(data))
{
int packetId = packet.ReadInt();
// Call appropriate method to handle the packet
_packetHandlers[packetId](packet);
}
});
}