{"record":{"id":"7618c6bbfcecf4b8","repo":"EllanJiang/GameFramework","slug":"object-is-invalid-objectpoolmanager-objectpool","errorCode":null,"errorMessage":"Object is invalid.","messagePattern":"Object is invalid\\.","errorType":"validation","errorClass":"GameFrameworkException","httpStatus":null,"severity":"error","filePath":"GameFramework/ObjectPool/ObjectPoolManager.ObjectPool.cs","lineNumber":195,"sourceCode":"                {\n                    return m_Priority;\n                }\n                set\n                {\n                    m_Priority = value;\n                }\n            }\n\n            /// <summary>\n            /// 创建对象。\n            /// </summary>\n            /// <param name=\"obj\">对象。</param>\n            /// <param name=\"spawned\">对象是否已被获取。</param>\n            public void Register(T obj, bool spawned)\n            {\n                if (obj == null)\n                {\n                    throw new GameFrameworkException(\"Object is invalid.\");\n                }\n\n                Object<T> internalObject = Object<T>.Create(obj, spawned);\n                m_Objects.Add(obj.Name, internalObject);\n                m_ObjectMap.Add(obj.Target, internalObject);\n\n                if (Count > m_Capacity)\n                {\n                    Release();\n                }\n            }\n\n            /// <summary>\n            /// 检查对象。\n            /// </summary>\n            /// <returns>要检查的对象是否存在。</returns>\n            public bool CanSpawn()\n            {","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/EllanJiang/GameFramework/blob/d0c010b05167c58e92350449d04864a91ca13fd2/GameFramework/ObjectPool/ObjectPoolManager.ObjectPool.cs#L177-L213","documentation":"ObjectPool.Register adds an externally created object to the pool's dictionaries keyed by object Name and Target. A null obj cannot be keyed or pooled, so Register throws 'Object is invalid.' before delegating to Object<T>.Create (which has the same guard).","triggerScenarios":"Calling objectPool.Register(null, spawned) directly; more often indirectly when a load/factory call returned null and its result was registered without checking.","commonSituations":"Asset or prefab load failures returning null; async initialization completing late so the object reference is still null at Register time; upstream service returning null on cache miss.","solutions":["Null-check the object before calling Register and handle load failure separately","Make the object producer non-null-guaranteed or return a failure indicator","Log which loader returned null and fix that resource path"],"exampleFix":"// before\nobjectPool.Register(CreateObject(), false); // CreateObject() can return null\n// after\nvar obj = CreateObject();\nif (obj == null) { Log.Error(\"Failed to create pooled object.\"); return; }\nobjectPool.Register(obj, false);","handlingStrategy":"validation","validationCode":"if (obj == null) { Log.Error(\"Refusing to register null object\"); return; }\nobjectPool.Register(obj, spawned);","typeGuard":"bool IsRegisterable<T>(T obj) where T : class => obj != null;","tryCatchPattern":"try { objectPool.Register(obj, spawned); }\ncatch (GameFrameworkException ex) { Log.Error(\"ObjectPool.Register got invalid object: {0}\", ex.Message); }","preventionTips":["Null-check before every Register call","Treat null loader results as errors upstream","Never pass async-pending references to Register"],"tags":["objectpool","null-argument","gameframework"],"backgroundTag":"null-argument","analyzedSha":"d0c010b05167c58e92350449d04864a91ca13fd2","analyzedAt":"2026-09-15T13:37:15.352Z","contentChangedAt":"2026-09-15T13:37:15.352Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}