Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

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


    Sep 29, 2016
    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);

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

    m_Driver.BeginSend(c, out DataStreamWriter 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)