{"record":{"id":"8c47a2d277bb0422","repo":"egametang/ET","slug":"cant-convert-to-entityref-entity-instanceid-0","errorCode":null,"errorMessage":"cant convert to entityref, entity instanceid == 0!","messagePattern":"cant convert to entityref, entity instanceid == 0!","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Packages/cn.etetet.core/Scripts/Core/Share/Entity/EntityRef.cs","lineNumber":146,"sourceCode":"    public struct EntityWeakRef<T>: IDisposable, IEquatable<EntityWeakRef<T>> where T: Entity\n    {\n        private long instanceId;\n        // 使用WeakReference，这样不会导致entity dispose了却无法gc的问题\n        // 不过暂时没有测试WeakReference的性能\n        private readonly WeakReference<T> weakRef;\n\n        private EntityWeakRef(T t)\n        {\n            if (t == null)\n            {\n                this.instanceId = 0;\n                this.weakRef = null;\n                return;\n            }\n\n            if (t.InstanceId == 0)\n            {\n                throw new Exception(\"cant convert to entityref, entity instanceid == 0!\");\n            }\n            this.instanceId = t.InstanceId;\n            this.weakRef = new WeakReference<T>(t);\n        }\n        \n        public void Dispose()\n        {\n            T t = this.Entity;\n            \n            if (t == null)\n            {\n                return;\n            }\n\n            t.Dispose();\n        }\n        \n        public T Entity","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/egametang/ET/blob/5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f/Packages/cn.etetet.core/Scripts/Core/Share/Entity/EntityRef.cs#L128-L164","documentation":"Thrown by the EntityWeakRef<T> constructor when wrapping an entity with InstanceId==0. Even a weak reference requires a live (InstanceId!=0) target so the dereference can validate liveness; wrapping a disposed entity is rejected.","triggerScenarios":"Constructing EntityWeakRef on a disposed entity; building a weak cache entry for an entity that was already recycled; capturing disposed entities into a weak reference list.","commonSituations":"Weak caches populated from entities that get disposed before insertion; deferred callbacks building weak refs for senders that died.","solutions":["Validate `t != null && t.InstanceId != 0` before constructing EntityWeakRef.","Drop cache entries whose InstanceId is 0 before they are wrapped.","Avoid constructing refs inside cleanup paths where the entity may already be disposed."],"exampleFix":"// before\nvar wr = new EntityWeakRef<T>(entity);\n\n// after\nif (entity == null || entity.InstanceId == 0) return;\nvar wr = new EntityWeakRef<T>(entity);","handlingStrategy":"type-guard","validationCode":"if (entity != null && entity.InstanceId != 0)\n{\n    var wr = new EntityWeakRef<T>(entity);\n}","typeGuard":"static bool IsLive<T>(T e) where T : Entity\n    => e != null && e.InstanceId != 0;","tryCatchPattern":null,"preventionTips":["Validate InstanceId != 0 before constructing EntityWeakRef.","Prune cache entries whose InstanceId reset to 0.","Skip weak-ref construction inside dispose callbacks."],"tags":["entity","entityweakref","disposed","core"],"backgroundTag":null,"analyzedSha":"5cab01f7a8bee5f49f4781eebe9e2b1c6d7ebe0f","analyzedAt":"2026-08-13T21:10:40.377Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}