Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    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)