UnsafeList hard crashes sometimes on Dispose / Destroy

Does anyone know how to workaround it?
Its identical to Crashes in freeing native memory B10.

But I’m using Unity 2020.2.7f1 / Collections 0.15.0-preview.21, here’s a stack:

Stacktrace:
  at <unknown> <0xffffffff>
  at (wrapper managed-to-native) Unity.Collections.LowLevel.Unsafe.UnsafeUtility.Free (void*,Unity.Collections.Allocator) [0x00008] in <42a5878ce129403083acccf18e43363f>:0
  at Unity.Collections.Memory/Unmanaged/Array.Resize (void*,long,long,Unity.Collections.Allocator,long,int) [0x0004d] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\Memory.cs:84
  at Unity.Collections.Memory/Unmanaged.Free (void*,Unity.Collections.Allocator) [0x00006] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\Memory.cs:25
  at Unity.Collections.AllocatorManager.TryLegacy (Unity.Collections.AllocatorManager/Block&) [0x00081] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\AllocatorManager.cs:437
  at Unity.Collections.AllocatorManager.Try (Unity.Collections.AllocatorManager/Block&) [0x0001c] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\AllocatorManager.cs:456
  at Unity.Collections.AllocatorManager.Free (Unity.Collections.AllocatorManager/AllocatorHandle,void*,int,int,int) [0x0004f] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\AllocatorManager.cs:74
  at Unity.Collections.AllocatorManager.Free (Unity.Collections.AllocatorManager/AllocatorHandle,void*) [0x00000] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\AllocatorManager.cs:85
  at Unity.Collections.LowLevel.Unsafe.UnsafeList.Dispose () [0x0000d] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\UnsafeList.cs:198
  at Unity.Collections.LowLevel.Unsafe.UnsafeList.Destroy (Unity.Collections.LowLevel.Unsafe.UnsafeList*) [0x0000c] in D:\Coding\ECSBoids\Library\PackageCache\com.unity.collections@0.15.0-preview.21\Unity.Collections\UnsafeList.cs:166
  at Octree.NativeOctree`1<EBoids.Data.BoidData>.Dispose () [0x00000] in D:\Coding\ECSBoids\Assets\_Code\Plugins\Octree\NativeOctree.cs:323
  at EBoids.Systems.BoidLookupSystem.OnDestroy () [0x00013] in D:\Coding\ECSBoids\Assets\_Code\Plugins\EBoids\Systems\BoidLookupSystem.cs:112
  at Unity.Entities.ComponentSystemBase.OnDestroy_Internal () [0x00000] in D:\Coding\ECSBoids\Packages\com.unity.entities@0.17.0-preview.41\Unity.Entities\ComponentSystemBase.cs:198
  at Unity.Entities.World.DestroyAllSystemsAndLogException () [0x0004d] in D:\Coding\ECSBoids\Packages\com.unity.entities@0.17.0-preview.41\Unity.Entities\World.cs:413
  at Unity.Entities.World.Dispose () [0x00031] in D:\Coding\ECSBoids\Packages\com.unity.entities@0.17.0-preview.41\Unity.Entities\World.cs:156
  at Unity.Entities.World.DisposeAllWorlds () [0x00002] in D:\Coding\ECSBoids\Packages\com.unity.entities@0.17.0-preview.41\Unity.Entities\World.cs:174
  at Unity.Entities.DefaultWorldInitialization.DomainUnloadOrPlayModeChangeShutdown () [0x00048] in D:\Coding\ECSBoids\Packages\com.unity.entities@0.17.0-preview.41\Unity.Entities\DefaultWorldInitialization.cs:94
  at Unity.Entities.DefaultWorldInitializationProxy.OnDisable () [0x00008] in D:\Coding\ECSBoids\Packages\com.unity.entities@0.17.0-preview.41\Unity.Entities\DefaultWorldInitializationProxy.cs:29
  at (wrapper runtime-invoke) object.runtime_invoke_void__this__ (object,intptr,intptr,intptr) [0x00020] in <eae584ce26bc40229c1b1aa476bfa589>:0

Address Stack:

Received signal SIGSEGV
Stack trace:
0x00007ff7365e7d14 (Unity) tlsf_free
0x00007ff732f3dc87 (Unity) DynamicHeapAllocator::smile:eallocate
0x00007ff732f39736 (Unity) DynamicHeapAllocator::TryDeallocate
0x00007ff732f394a1 (Unity) DualThreadAllocator<DynamicHeapAllocator>::TryDeallocate
0x00007ff732f1bb42 (Unity) MemoryManager::smile:eallocate
0x00007ff732f2da6a (Unity) free_alloc_internal
0x00007ff73415c5f5 (Unity) UnsafeUtility::Free
0x00007ff73414a183 (Unity) UnsafeUtility_CUSTOM_Free
0x000001f9c83f86dd (Mono JIT Code) (wrapper managed-to-native) Unity.Collections.LowLevel.Unsafe.UnsafeUtility:Free (void*,Unity.Collections.Allocator)
0x000001f9c83d9423 (Mono JIT Code) [Memory.cs:85] Unity.Collections.Memory/Unmanaged/Array:Resize (void*,long,long,Unity.Collections.Allocator,long,int)
0x000001f9c83f863b (Mono JIT Code) [Memory.cs:26] Unity.Collections.Memory/Unmanaged:Free (void*,Unity.Collections.Allocator)
0x000001f9c83db5bb (Mono JIT Code) [AllocatorManager.cs:438] Unity.Collections.AllocatorManager:TryLegacy (Unity.Collections.AllocatorManager/Block&)
0x000001f9c83db373 (Mono JIT Code) [AllocatorManager.cs:456] Unity.Collections.AllocatorManager:Try (Unity.Collections.AllocatorManager/Block&)
0x000001f9c83dbb3b (Mono JIT Code) [AllocatorManager.cs:74] Unity.Collections.AllocatorManager:Free (Unity.Collections.AllocatorManager/AllocatorHandle,void*,int,int,int)
0x000001f9c83dba2b (Mono JIT Code) [AllocatorManager.cs:86] Unity.Collections.AllocatorManager:Free (Unity.Collections.AllocatorManager/AllocatorHandle,void*)
0x000001f9c868f09b (Mono JIT Code) [UnsafeList.cs:199] Unity.Collections.LowLevel.Unsafe.UnsafeList:smile:ispose ()
0x000001f9c868ef6b (Mono JIT Code) [UnsafeList.cs:167] Unity.Collections.LowLevel.Unsafe.UnsafeList:smile:estroy (Unity.Collections.LowLevel.Unsafe.UnsafeList*)
0x000001f902fc5e73 (Mono JIT Code) [NativeOctree.cs:324] Octree.NativeOctree`1<EBoids.Data.BoidData>:smile:ispose ()
0x000001f902fc5c5b (Mono JIT Code) [BoidLookupSystem.cs:113] EBoids.Systems.BoidLookupSystem:OnDestroy ()
0x000001f902fc10b5 (Mono JIT Code) [ComponentSystemBase.cs:199] Unity.Entities.ComponentSystemBase:OnDestroy_Internal ()
0x000001f902fc0ca3 (Mono JIT Code) [World.cs:414] Unity.Entities.World:smile:estroyAllSystemsAndLogException ()
0x000001f902fc00b3 (Mono JIT Code) [World.cs:157] Unity.Entities.World:smile:ispose ()
0x000001f74679fe9b (Mono JIT Code) [World.cs:172] Unity.Entities.World:smile:isposeAllWorlds ()
0x000001f9c870e6cb (Mono JIT Code) [DefaultWorldInitialization.cs:96] Unity.Entities.DefaultWorldInitialization:smile:omainUnloadOrPlayModeChangeShutdown ()
0x000001f74679f93b (Mono JIT Code) [DefaultWorldInitializationProxy.cs:30] Unity.Entities.DefaultWorldInitializationProxy:OnDisable ()
0x000001f9c83eb660 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007ffdc698e480 (mono-2.0-bdwgc) [mini-runtime.c:2812] mono_jit_runtime_invoke
0x00007ffdc6912902 (mono-2.0-bdwgc) [object.c:2921] do_runtime_invoke
0x00007ffdc691b95f (mono-2.0-bdwgc) [object.c:2968] mono_runtime_invoke
0x00007ff733e20d64 (Unity) scripting_method_invoke
0x00007ff733e196e5 (Unity) ScriptingInvocation::Invoke
0x00007ff733e19afe (Unity) ScriptingInvocation::InvokeChecked
0x00007ff733e96666 (Unity) SerializableManagedRef::CallMethod
0x00007ff733de579a (Unity) MonoBehaviour::RemoveFromManager
0x00007ff732fc7438 (Unity) GameObject::ActivateAwakeRecursivelyInternal
0x00007ff732fc7049 (Unity) GameObject::ActivateAwakeRecursively
0x00007ff732fc9c49 (Unity) GameObject::smile:eactivate
0x00007ff7336f290e (Unity) DestroyObjectHighLevel_Internal
0x00007ff7336f2534 (Unity) DestroyObjectHighLevel
0x00007ff733700b97 (Unity) DestroyWorldObjects
0x00007ff73515a8db (Unity) EditorSceneManager::RestoreSceneBackups
0x00007ff734b06cbd (Unity) PlayerLoopController::ExitPlayMode
0x00007ff734b1c1e7 (Unity) PlayerLoopController::SetIsPlaying
0x00007ff734b1ef44 (Unity) Application::TickTimer
0x00007ff735485f51 (Unity) MainMessageLoop
0x00007ff73548a001 (Unity) WinMain
0x00007ff7372f9576 (Unity) __scrt_common_main_seh
0x00007ffe08127034 (KERNEL32) BaseThreadInitThunk
0x00007ffe08ec2651 (ntdll) RtlUserThreadStart

NativeList i just an UnsafeList under the hood so any issue should also exist in that.
And it’s the same Free function that is used by every single collection.

Personally I’ve never had an issue disposing a UnsafeList that wasn’t my fault and I use them quite regularly.
I’m more inclined to think this is user error than a bug but without seeing code there’s not much to say.

This is the cost of using unsafe code~

What platform is this happening on?

Its in Win64 / Editor when exiting playmode.

It could also be my fault, but not quite sure what’s causing this.

So, I’ve tried running -debugallocator (from advice in this post ), seems like this could be caused by domain code, and memory corruption is actually in a different place.

Got this instead:

NullReferenceException: Object reference not set to an instance of an object
System.Buffer.memcpy4 (System.Byte* dest, System.Byte* src, System.Int32 size) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.Buffer.Memcpy (System.Byte* dest, System.Byte* src, System.Int32 size) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
System.String.memcpy (System.Byte* dest, System.Byte* src, System.Int32 size) (at <eae584ce26bc40229c1b1aa476bfa589>:0)
Unity.Collections.LowLevel.Unsafe.UnsafeUtility.WriteArrayElement[T] (System.Void* destination, System.Int32 index, T value) (at <42a5878ce129403083acccf18e43363f>:0)

Which points to this line:

UnsafeUtility.WriteArrayElement(_elements -> Ptr, node.FirstChildIndex + node.Count, element);

No clue why it happens though, need to figure it out.