Leak detected when trying to create server world

Hello,
I’m trying to set up a simple host with netcode for entities. I have stripped down my code to literally do the following:

ClientServerBootstrap.AutoConnectPort = 7777;
_serverWorld = ClientServerBootstrap.CreateServerWorld(“ServerWorld”);
// wait for a button press…
_serverWorld.Dispose();

The problem is that upon exiting playmode and then entering again I get:
Leak Detected : Persistent allocates 1 individual allocations.

Ok, I turned on Jobs leaks callstacks, but it somehow is incomplete, like if it was arbitrarily truncated, so I can’t actually figure out what is causing the leak. The callstack I get is this:

Found 1 leak(s) from callstack:
0x00007ffdcc7b2e36 (ba8b60678abccfe6abaed1ac343d43d) Unity.Entities.EntityComponentStore.CreateEntities (at C:/work/Test/Robot Survival (Unity Netcode for Entities)/Library/PackageCache/com.unity.burst/.Runtime/Library/PackageCache/com.unity.entities/Unity.Entities/EntityComponentStoreCreateDestroyEntities.cs:30)
0x00007ffdcc7beccf (ba8b60678abccfe6abaed1ac343d43d) c2a99021e1b9003db67e0e5cba47df11
0x0000027655d8cfd5 (Mono JIT Code) (wrapper managed-to-native) Unity.Entities.StructuralChange/Unity.Entities.CreateEntity_000011E9$BurstDirectCall:wrapper_native_indirect_00000276A3EBFC68 (intptr&,Unity.Entities.EntityComponentStore*,void*,Unity.Entities.Entity*,int)
0x0000027655d8cb03 (Mono JIT Code) Unity.Entities.StructuralChange/Unity.Entities.CreateEntity_000011E9$BurstDirectCall:Invoke (Unity.Entities.EntityComponentStore*,void*,Unity.Entities.Entity*,int)
0x0000027655d8c9b3 (Mono JIT Code) Unity.Entities.StructuralChange:CreateEntity (Unity.Entities.EntityComponentStore*,void*,Unity.Entities.Entity*,int) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/StructuralChange.cs:149)
0x0000027655d8be93 (Mono JIT Code) Unity.Entities.EntityDataAccess:CreateEntityDuringStructuralChange (Unity.Entities.EntityArchetype,Unity.Entities.Entity*,int,Unity.Entities.SystemHandle&) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/EntityDataAccess.cs:712)
0x0000027655d8ba9b (Mono JIT Code) Unity.Entities.EntityDataAccess:CreateEntityDuringStructuralChange (Unity.Entities.EntityArchetype,Unity.Entities.SystemHandle&) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/EntityDataAccess.cs:687)
0x0000027655d8b6e3 (Mono JIT Code) Unity.Entities.EntityManager:CreateEntity (Unity.Entities.EntityArchetype) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/EntityManager.cs:3228)
0x0000027655d7353b (Mono JIT Code) Unity.Entities.WorldUnmanagedImpl:CreateSystemEntity (Unity.Entities.World,int,Unity.Entities.SystemState*) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/WorldUnmanaged.cs:514)
0x0000027655d7212b (Mono JIT Code) Unity.Entities.WorldUnmanagedImpl:CreateUnmanagedSystemInternal (Unity.Entities.World,int,long,int,void*&,bool) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/WorldUnmanaged.cs:472)
0x0000027655d713eb (Mono JIT Code) Unity.Entities.WorldUnmanagedImpl:CreateUnmanagedSystem (Unity.Entities.SystemTypeIndex,long,bool) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/WorldUnmanaged.cs:592)
0x0000027655d70e93 (Mono JIT Code) Unity.Entities.WorldUnmanagedImpl:CreateUnmanagedSystem (Unity.Entities.SystemTypeIndex,bool) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/WorldUnmanaged.cs:674)
0x0000027655d70ca3 (Mono JIT Code) Unity.Entities.WorldUnmanaged:CreateUnmanagedSystem (Unity.Entities.SystemTypeIndex,bool) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/WorldUnmanaged.cs:1105)
0x0000027655d6bd83 (Mono JIT Code) Unity.Entities.World:GetOrCreateSystemsAndLogException (Unity.Collections.NativeList`1<Unity.Entities.SystemTypeIndex>,int,Unity.Collections.AllocatorManager/AllocatorHandle) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/World.cs:1229)
0x0000027655d6b2fb (Mono JIT Code) Unity.Entities.World:GetOrCreateSystemsAndLogException (Unity.Collections.NativeList`1<Unity.Entities.SystemTypeIndex>,Unity.Collections.AllocatorManager/AllocatorHandle) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/World.cs:1321)
0x0000027655d67d6b (Mono JIT Code) Unity.Entities.DefaultWorldInitialization:AddSystemToRootLevelSystemGroupsInternal<Unity.Entities.DefaultWorldInitialization/DefaultRootGroups> (Unity.Entities.World,Unity.Collections.NativeList`1<Unity.Entities.SystemTypeIndex>,Unity.Entities.ComponentSystemGroup,Unity.Entities.DefaultWorldInitialization/DefaultRootGroups) (at ./Library/PackageCache/com.unity.entities/Unity.Entities/DefaultWorldInitialization.cs:252)
0x0000027655d511bb (Mono JIT Code) Unity.Entities.DefaultWorldInitialization:AddSystemToRootLevelSystemGroupsInternal (Unity.Entities.World,Uni

Any advice would be greatly appreciated.

In my experience you can simply ignore all those “leak detected”, particularly if nothing indicates that it’s coming from your code.

You could, and should, make sure you run the latest package and editor versions. These leaks get fixed over time, while with new code new ones may appear.

Are you running the editor in debug mode? Not sure if this affects callstacks. You can toggle this in the bottom right, or simply attach the IDE debugger once and Unity will ask you to restart the editor in Debug mode.

Sure I am (I consider myself a programmer after all :wink:). As one can see, the callstack is there, it’s just abruptly truncated so that I can’t see its origins where my code CAN potentially be.

New entities releases come with fixes for things like memory leaks. If you’re not on the latest releases (1.2.4 and 1.3.2, the latter is preferred), consider updating. 1.2.4 in particular has a changelog entry mentioning a fix for an EntityComponentStore leak, and it looks like this had been included in the 1.3.0-exp.1 update as well. If you still see leaks (or are already on one of these releases), submit a bug report.

I should’ve stated this in the original post: I’m on Unity 6, entities/entities-graphics/netcode-for-entities/ are all 1.3.2

Ok, I’ve distilled it down to the following:

public class Main : MonoBehaviour
{
	private IEnumerator Start()
	{
		World serverWorld = ClientServerBootstrap.CreateServerWorld("ServerWorld");
		yield return new WaitForSeconds(2);
		serverWorld.Dispose();
		Debug.Log("Disposed");
	}
}

Playing this in editor twice demonstrates the leak that I’m experiencing.

I’m not able to reproduce the issue with the above code. For reference, I have a blank URP template project on 6000.0.19f1 with Entities Graphics and Netcode for Entities 1.3.2. I tried using entity store v1, which didn’t exhibit any issues either. Is there a chance you could produce a minimal repro project? That would be useful in your submission of a bug report, and sharing it would help others diagnose this here.

Ok, here’s my project, it’s Unity 6000.21 though…
LeakDemo.zip (48.0 KB)

Open scene Main, hit play once, wait for it to say “Disposed” in the console. Then stop and play again - here goes the leak.

Thanks, the project demonstrates a leak on my machine. It looks like it comes down to missing a call to DefaultWorldInitialization.RegisterUnloadOrPlayModeChangeShutdown, a private method that’s called by DefaultWorldInitialization.Initialize. Since your project specifies UNITY_DISABLE_AUTOMATIC_SYSTEM_BOOTSTRAP_RUNTIME_WORLD, one of the points where DefaultWorldInitialization.Initialize gets called will no longer be compiled. As far as I know, the only other point that this gets called by default is from edit mode when there’s a non-empty subscene (creates “Editor World”), so at present you would need to call it manually outside that case.

The simplest thing to do (a dumb workaround) would be to call DefaultWorldInitialization.Initialize("temp", true).Dispose(); at some point during runtime, which would set up the appropriate state in DefaultWorldInitialization for it to cleanup the allocations before domain reload. A “better” thing to do would be embed the package and make DefaultWorldInitialization.RegisterUnloadOrPlayModeChangeShutdown accessible so you can call it as necessary, to ensure the entity store allocations will be freed.

This behavior does seem like a bug, as the shared entity store data still requires cleanup when any custom worlds (even just default netcode worlds) are created without using DefaultWorldInitialization.Initialize. Consider submitting a bug report if you haven’t already.

1 Like

Kudos man, it IS the solution. I have submitted a bug report as you suggested. Would you please explain how you were able to figure out what’s going on?

1 Like

Sure thing. The leak would (obviously) be coming from failure to dispose an allocation. I checked the call stack of the leak (without Burst for more clarity) and landed on EntityComponentStoreEntities.cs:228. The data is managed by EntityComponentStore’s SharedStatic of an internal type EntityComponentStore.EntityStore, which conveniently has a Dispose method freeing all the allocations that’s only called in DefaultWorldInitialization.DomainUnloadOrPlayModeChangeShutdown. DomainUnloadOrPlayModeChangeShutdown has one call in DefaultWorldInitialization.cs:44 (CleanupWorldBeforeSceneLoad, wouldn’t be the final cleanup so not what we want) and another in DefaultWorldInitializationProxy.OnDisable. That gets created in DefaultWorldInitialization.RegisterUnloadOrPlayModeChangeShutdown. This seems to be responsible for setting up disposal on critical events like domain reload. Since that method only gets called in DefaultWorldInitialization.Initialize (and is unconditionally called there) and Initialize is typically called to populate World.DefaultGameObjectInjectionWorld, I figured auto bootstrap was disabled at some level.

It’s a bit unfortunate the package code is written this way. I hope your bug report gets a reasonably short turnaround. You can post the initial issue ID (IN-X…) and any subsequent IDs (e.g. ECSB-X…) / issue tracker page links here for people to be aware of. Developers working on these packages do drop in and read posts, so you can help them find the issue quickly.

1 Like

Ok, for now I’ve only got the initial issue id: IN-85541

1 Like