{"record":{"id":"9a2c0ef1527eefae","repo":"sschmid/Entitas","slug":"contextstillhasretainedentitiesexception-this","errorCode":null,"errorMessage":"ContextStillHasRetainedEntitiesException(this, _retainedEntities)","messagePattern":"ContextStillHasRetainedEntitiesException\\(this, _retainedEntities\\)","errorType":"exception","errorClass":"ContextStillHasRetainedEntitiesException","httpStatus":null,"severity":"error","filePath":"src/Entitas/Context/Context.cs","lineNumber":228,"sourceCode":"            entity.OnDestroyEntity += _OnDestroyEntityDelegate;\n\n            OnEntityCreated?.Invoke(this, entity);\n\n            return entity;\n        }\n\n        /// Destroys all entities in the context.\n        /// Throws an exception if there are still retained entities.\n        public void DestroyAllEntities()\n        {\n            var entities = GetEntities();\n            for (var i = 0; i < entities.Length; i++)\n                entities[i].Destroy();\n\n            _entities.Clear();\n\n            if (_retainedEntities.Count != 0)\n                throw new ContextStillHasRetainedEntitiesException(this, _retainedEntities);\n        }\n\n        /// Determines whether the context has the specified entity.\n        public bool HasEntity(TEntity entity) => _entities.Contains(entity);\n\n        /// Returns all entities which are currently in the context.\n        public TEntity[] GetEntities()\n        {\n            return _entitiesCache ??= _entities.ToArray();\n        }\n\n        /// Returns all entities matching the specified matcher.\n        public TEntity[] GetEntities(IMatcher<TEntity> matcher)\n        {\n            return GetGroup(matcher).GetEntities();\n        }\n\n        /// Returns a group for the specified matcher.","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/sschmid/Entitas/blob/37547d1bd280f3f1c1ef7edb44f2a206660fbfb4/src/Entitas/Context/Context.cs#L210-L246","documentation":"DestroyAllEntities destroys all entities and clears the set, then asserts no entities remain retained (some object still holds a Retain). If retained entities exist, recycling bookkeeping would be corrupted, so Entitas throws instead of leaking them silently.","triggerScenarios":"Calling context.Reset() or context.DestroyAllEntities() while external systems (indexes, pools, EntityLink, custom retainers) still hold Retain on entities that were destroyed.","commonSituations":"Forgetting to Unlink EntityLink components before context teardown, custom entity indexes retaining entities, or editor/test teardown not releasing retained entities.","solutions":["Ensure all retainers Release entities (e.g. Unlink all EntityLinks) before DestroyAllEntities","Clear/reset all entity indexes and pools before resetting the context","Fix asymmetry where Retain is called without a matching Release"],"exampleFix":"// before\ncontext.Reset(); // throws if EntityLinks still retain entities\n// after\nforeach (var link in Object.FindObjectsOfType<EntityLink>())\n    link.Unlink();\ncontext.Reset();","handlingStrategy":"validation","validationCode":"foreach (var link in Object.FindObjectsOfType<EntityLink>())\n    if (link.entity != null) link.Unlink();\ncontext.DestroyAllEntities();","typeGuard":null,"tryCatchPattern":"try { context.Reset(); }\ncatch (Exception e) { Debug.LogError(\"Retained entities remain: release all retainers before reset\"); }","preventionTips":["Unlink all EntityLinks before teardown","Reset entity indexes before destroying entities","Symmetric Retain/Release in all systems"],"tags":["entitas","context","retain-release","teardown"],"backgroundTag":"invalid-state-transition","analyzedSha":"37547d1bd280f3f1c1ef7edb44f2a206660fbfb4","analyzedAt":"2026-09-14T00:26:23.246Z","contentChangedAt":"2026-09-14T00:26:23.246Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}