Implement a custom native container
Implement custom native containers.
Read time 3 minutesLast updated 5 days ago
To implement a custom native container, you must annotate your type with the the attribute. You should also understand how native containers are integrated with the safety system.
NativeContainerThere are two major elements to implement:
- Usage tracking: Allows Unity to keep track of scheduled jobs that use a instance, so that it can detect and prevent potential conflicts, such as two jobs writing to the same native container at the same time.
NativeContainer - Leak tracking: Detects when a isn’t disposed of properly. In this situation, a memory leak happens, where the memory allocated to the
NativeContainerbecomes unavailable for the entire remaining lifetime of the program.NativeContainer
Implement usage tracking
To access usage tracking in your code, use the class. holds a reference to the central information that the safety system stores for a given native container, and is the main way that the methods of a interact with the safety system. Because of this, every instance must contain an field named .
AtomicSafetyHandleAtomicSafetyHandleNativeContainerNativeContainerAtomicSafetyHandlem_SafetyEach stores a set of flags that indicate what types of operation can be performed on the native container in the current context. When a job contains a instance, the job system automatically configures the flags in the to reflect the way that the native container can be used in that job.
AtomicSafetyHandleNativeContainerAtomicSafetyHandleWhen a job tries to read from a instance, the job system calls the method before reading, to confirm that the job has read access to the native container. Similarly, when a job tries to write to a native container, the job system calls before writing, to check that the job has write access to the native container. Two jobs that have been assigned the same instance have separate objects for that native container, so although they both reference the same set of central information, they can each hold separate flags that indicate what read and write access each job has to the native container.
NativeContainerCheckReadAndThrowCheckWriteAndThrowNativeContainerAtomicSafetyHandleImplement leak tracking
Unity's native code primarily implements leak tracking. It uses the method to allocate the memory needed to store data, and then uses to dispose of it.
UnsafeUtility.MallocTrackedNativeContainerUnsafeUtility.FreeTrackedIn earlier versions of Unity the class provides leak tracking. Unity reports a memory leak when the garbage collector collects the object. To create a , use the method, which also initializes the at the same time. When you use this method, you don’t need to initialize the . When the is disposed of, the method disposes of both the and the in a single call.
DisposeSentinelDisposeSentinelDisposeSentinelCreateAtomicSafetyHandleAtomicSafetyHandleNativeContainerDisposeDisposeSentinelAtomicSafetyHandleTo identify where the leaked was created, you can capture the stack trace of where the memory was originally allocated. To do this, use the property. You can also access this property in the Editor. To do this, go to Preferences > Jobs > Leak Detection Level and choose the leak detection level you need.
NativeContainerNativeLeakDetection.ModeNested native containers
The safety system doesn’t support nested native containers in jobs, because the job system can't correctly configure the for each individual inside the larger instance.
AtomicSafetyHandleNativeContainerNativeContainerTo prevent scheduling jobs that use nested native containers, use , which flags a as nested when they contain other instances.
SetNestedContainerNativeContainerNativeContainerSafety IDs and error messages
The safety system provides error messages that indicate when your code doesn't adhere to safety constraints. To help make the error messages clearer, you can register a object's name with the safety system.
NativeContainerTo register a name, use , which returns a safety ID that you can pass to . Once you create a safety ID, you can reuse it for all instances of the , so a common pattern is to store it in a static member of the container class.
NewStaticSafetyIdSetStaticSafetyIdNativeContainerYou can also override the error messages for specific safety constraint violations with .
SetCustomErrorMessage