Unhandled exception crashes the game : A heap has been corrupted

Hello,

I make a Windows game that uses named pipes to communicate with the outside. I can successfully receive data, but the game keeps crashing when I send ~5 messages (yes, 4 is fine but the fifth one makes everything crash, even if messages are identical… I don’t know why).

What happens is a small delay of a second and then the game just closes itself, with no information.

I tried :

  • Running a “development build” standalone, but the error is not catched into the output log.
  • Different ways of sending my message (ASync, normal, by disposing NamedPipeClientStream …) unsuccessfull.
  • Running from the Editor. In this case I can use Visual Studio to debug and I get the error

“Unhandled exception at …
(ntdll.dll) in Unity.exe: …: A
heap has been corrupted (parameters:
…).”

I have no idea what is happening here. My hypothesis is that unity’s System.IO.Pipes is broken.

I would like to find a workaround or if possible to ignore this error. I am already using try-catch when sending messages and it seems like it does nothing. When running from the editor and debugging with VS, I was able to ignore the error and continue the execution. Maybe I can do that to automatically ?

Here is the send method I use now :

public void send(PipeMessage message)
{
    try {
        pipeClient = new NamedPipeClientStream(".", sendPipeName, PipeDirection.Out, PipeOptions.Asynchronous);
        pipeClient.Connect(timeOut);
        pipeClient.BeginWrite(message.bytes, 0, message.bytes.Length, aSyncSend, pipeClient);
    }
        catch (Exception e) {
    }
}

private void aSyncSend(IAsyncResult iar)
{
    try
    {
        NamedPipeClientStream sApipeClient = (NamedPipeClientStream)iar.AsyncState;
        sApipeClient.EndWrite(iar);
        sApipeClient.Flush();
    }
        catch (Exception e) {
    }
}

I solved it. There is clearly an issue with Unity’s pipes or streams, but I made it working by creating a single instance of NamedPipeServerStream and NamedPipeClientStream and reusing them on each iteration.

Imo, this would be the way to do, but on every example I found a new instance was created each time.