Skip to content

Release vs. KENSEI_DEBUG ​

KenseiECS has two builds of the same code. Release keeps the hot paths free of checks; KENSEI_DEBUG compiles in validation that turns misuse into exceptions.

SituationReleaseKENSEI_DEBUG
Add of a component the entity already hasthrowsthrows
Add/Get/Has/Remove, AddEvent, Subscribe/Unsubscribe with a dead or stale Entityundefined (may corrupt another entity)throws, also while another entity is being destroyed; Add/Get/Has/Remove and AddEvent accept the entity whose DestroyEntity is running, for its listeners
Pool Add(int) on a dead slotundefinedthrows, except for the slot being destroyed
Get (world or pool) or pool Modify of a component the entity does not haveIndexOutOfRangeException on Mono and .NET; on IL2CPP, which skips bounds checks there, a read outside the pool's arrays, and a write through the ref corrupts memorythrows
GetComponentTypes / GetComponentCount of a dead entityreads the slot: empty while it is free, another entity's components once it is reusedthrows
A listener re-adds a component to the entity being destroyed on every removal passDestroyEntity never returnsthrows after 1,000 passes
Remove of a missing componentno-opno-op
A filter with Exc only, or with no constraints at all (world.Filter<Exc<Died>>())End() throwsEnd() throws
world.Subscribe(entity, view) without the listener type (also Unsubscribe, UnsubscribeLater, CreateWithListener)not checked: C# infers the view's class, so the call works on a Listeners<ViewClass> that no system reads and the view is never callednot checked
world.AddEvent of a type with no OneFrame<EventBuffer<T>>() or DelHere registrationnot checked: the events are never removed, and every consumer reads them again each framenot checked
DestroyEntity of a dead entityno-opno-op
CopyEntity of a dead entityreturns Entity.Nullthrows
GetEntity on a dead slotdead handledead handle
A CommandBuffer command on a PendingEntity of another buffer, of a recording the buffer has played back or cleared since (also right after Playback, before anything else is recorded), or a default onenot checked: the command goes to whichever entity has that id in the buffer, or to the one an earlier Playback created under itthrows; the command is not recorded
Removing an unvisited entity from an iterated filter (would double-visit)double visitthrows once the removal is complete: the entity has left the filter, the other filters are updated and an entity left without components is destroyed, so the world is consistent after the exception (up to 2.4.0 it threw in the middle: the entity stayed in the iterated filter without the component). Inside DestroyEntity the destroy is interrupted like by any exception: the slot is released and the components not removed yet stay orphaned until Clear
A Filter.Enumerator driven by hand (GetEnumerator, MoveNext) and never disposednothing to checkWorld.NextTick outside a Run, or the end of the outermost SystemsRunner.Run, releases the filter's iteration guard and throws, naming the filter; up to 2.4.0 the guard stayed held and later removals from the filter threw the double-visit error at random. A foreach over a filter that is wrapped around a root Run() or a World.NextTick(), outside any Run, looks the same from there and gets the same report: Run() throws from its NextTick before any system ran, so the systems are skipped for that frame, and the loop goes on with its guard released. Drive the frames from outside a loop over a filter
Runner: Run before Initnot checked: the systems run without their Initthrows
Runner: Add after Initnot checked: Init is not called for the systemthrows
Runner: unknown name in SetActive / IsActive / GetRunnerignored: does nothing, returns false, returns nullthrows
Runner: a name registered twice in one runnernot checked: lookups by the name reach only one of the twothrows
Runner: a runner that is already registered added again (any slot, any parent), or added to itself or to a runner nested in itthrowsthrows
EcsBootstrap: Configure registers something in the update runner under "fixed" or "late"not checked: the name reaches two registrationsAwake throws right after Configure
Runner: SetActive / IsActive on a system without Run (Init/Destroy only)does nothing / returns falsethrows
Nested runner with a different World / SharedDatasilently ignoredthrows
CreateEntity / Pool<T> after World.DestroyNullReferenceException on Mono and .NET; on IL2CPP, which skips null checks in World, a read through a null reference that crashes the player (an access violation, not an exception)ObjectDisposedException
World.Clear / ReserveEntities / any Reserve (world, pool, filter) / Filter after World.DestroyObjectDisposedExceptionObjectDisposedException
A reserved storage array grows past its reserve (ReserveEntities, pool.Reserve, filter.Reserve)not checkedcounted in GrowthsAfterReserve, the first one kept in FirstGrowthAfterReserve, logged by EcsProfiler while it records that world; growth of a pool or filter nothing reserved is not counted, and EcsProfiler logs it marked (not reserved)

Validation code is not compiled in release; the cost is zero.

csharp
var entity = world.CreateEntity(new Health { Value = 10 });
world.DestroyEntity(entity);

// Release: undefined; the slot may already belong to another entity.
// KENSEI_DEBUG: throws InvalidOperationException.
world.Get<Health>(entity);

// Safe in both modes: check first.
if (world.IsAlive(entity)) {
    world.Get<Health>(entity).Value -= 1;
}

TIP

Develop and run tests with KENSEI_DEBUG; a "may corrupt another entity" bug in release shows up there as an exception at the faulty call. Safe patterns for removing entities while iterating are covered in Filters and CommandBuffer.

Enabling it ​

  • Unity: toggle via KenseiECS -> Debug Mode. It sets KENSEI_DEBUG in the active Build Profile (Unity 6) or, without one, in the Player Settings of the active build target only. A Build Profile is a shared asset under version control, so the toggle logs each write to it. Turning the mode off does not touch other targets; a warning lists those whose Player Settings still have the define. A non-development player build with the define fails unless KENSEI_ALLOW_DEBUG_RELEASE is set the same way. The error names where the define comes from: the profile or the Player Settings of the target (checked in the preprocess step), or BuildPlayerOptions.extraScriptingDefines, Additional Compiler Arguments or Assets/csc.rsp (checked right after the player scripts compile). Content builds (Addressables, AssetBundles) are not checked. The checks run inside the build, after Unity has started calling preprocessors, so build scripts should check the defines before BuildPlayer; see Release build guard.
  • .NET / tests: the define has to reach the project that compiles the KenseiECS sources. With a project reference to KenseiECS.NET/KenseiECS.csproj, build with -p:KenseiDebug=true: it is a global property, so the referenced project gets it too. A project that compiles the sources itself, as KenseiECS.Tests does, adds KENSEI_DEBUG to its own DefineConstants; defining it in a test project that only references a built KenseiECS.dll changes nothing.

Debug-only features ​

Besides the checks, KENSEI_DEBUG enables the debug tooling: EcsProfiler, per-system timings in the runner, entity names, World.GrowthsAfterReserve and World.FirstGrowthAfterReserve. Code that uses them must compile in release too:

  • SetName, GetName, GrowthsAfterReserve and FirstGrowthAfterReserve exist in every build. Without the define they do nothing, return null, 0 and an empty StorageGrowth, so they are safe in any expression (SetName still evaluates its arguments).
  • EcsProfiler with ProfileEvent and ProfileEventType, the runner timings (SystemInfo.LastRunMs and PeakRunMs, SystemsRunner.ResetTimings) and the untyped GetRaw/SetRaw of a pool exist only under KENSEI_DEBUG, so code that uses them needs the #if:
csharp
world.SetName(entity, "Player");     // a no-op in release; arguments are still evaluated

#if KENSEI_DEBUG
EcsProfiler.Enable(world);           // EcsProfiler exists only under KENSEI_DEBUG
#endif