Search Unity

Question Intentional Memoryleak from undisposed NativArrays does not seem to create a Memoryleak

Discussion in 'Unity Transport' started by CrackHack, Jun 25, 2021.

  1. CrackHack

    CrackHack

    Joined:
    Sep 29, 2016
    Posts:
    7
    So you may think its weird to try to cause an intentional Memory-Leak. Mostly because it is, however to help illustrate my problem consider this code:

    m_Driver.BeginSend(c, out DataStreamWriter w1);
    w1.WriteUInt(0);
    m_Driver.EndSend(w1);

    NativeArray<byte> stream = writer.AsNativeArray();

    m_Driver.BeginSend(c, out DataStreamWriter w);
    w.WriteBytes(stream);
    m_Driver.EndSend(w);

    This code creates a small package containing 1 Uint and sends it twice. But im not Disposing the NativeArray. So that should give me a memory leak. Now the reason I dont dispose it is because i cant. its derived from a NativeList which means if I were to dispose it, the list would still be around. Now .EndSend() should dispose the list, however it clearly doesnt when .EndSend() is called because I can still use the data from it in the next package.

    So I dont manually dispose the NativeArray and I dont get a memoryLeak. Is that because the NativeList inside of BeginSend is only predisposed to be deleted, so I can still use the data because it hasnt been deleted yet but I also dont get the Leak because its deleted some other time? If so, when is the List being deleted?

    What way can I use to build a package to distribute it to multiple connections without doing a BeginSend() , copying the array, doing a AbortSend() and then sending that package out? (This doesnt need an answere because that workaround works^^, my other question needs an answere though)