{"record":{"id":"599374add562dd10","repo":"Unity-Technologies/UnityCsReference","slug":"you-shouldn-t-call-cleanup-for-editors-dispose-re","errorCode":null,"errorMessage":"You shouldn't call Cleanup for Editors. Dispose resources in Editor.OnDisable","messagePattern":"You shouldn't call Cleanup for Editors\\. Dispose resources in Editor\\.OnDisable","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"Editor/Mono/Inspector/Editor.cs","lineNumber":1516,"sourceCode":"\n            return true;\n        }\n\n        public virtual bool UseDefaultMargins()\n        {\n            return true;\n        }\n\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public void Initialize(UnityObject[] targets)\n        {\n            throw new InvalidOperationException(\"You shouldn't call Initialize for Editors\");\n        }\n\n        [EditorBrowsable(EditorBrowsableState.Never)]\n        public void Cleanup()\n        {\n            throw new InvalidOperationException(\"You shouldn't call Cleanup for Editors. Dispose resources in \" +\n                \"Editor.OnDisable\");\n        }\n\n        public bool MoveNextTarget()\n        {\n            referenceTargetIndex++;\n            return referenceTargetIndex < targets.Length;\n        }\n\n        public void ResetTarget()\n        {\n            referenceTargetIndex = 0;\n        }\n\n        // Implement this method to show a limited inspector for showing tweakable parameters in an Asset Store preview.\n        internal virtual void OnAssetStoreInspectorGUI()\n        {\n        }","sourceCodeStart":1498,"sourceCodeEnd":1534,"githubUrl":"https://github.com/Unity-Technologies/UnityCsReference/blob/225b0fbdb57cc17d094e8056b71f8314aba56f73/Editor/Mono/Inspector/Editor.cs#L1498-L1534","documentation":"Editor.Cleanup is the counterpart to Initialize: hidden via EditorBrowsable(Never) and throwing InvalidOperationException. Resource disposal for editors must happen in OnDisable, so calling Cleanup directly is disallowed.","triggerScenarios":"Explicitly calling editor.Cleanup(), or an auto-disposal/reflection routine that invokes public methods matching a cleanup convention.","commonSituations":"DI/container frameworks calling a Cleanup/Dispose-like method by convention. Code that pairs an Initialize call with a Cleanup call. Assuming Cleanup frees native resources.","solutions":["Do not call Cleanup; release resources in Editor.OnDisable.","If a container requires explicit teardown, route it to destroy the editor (DestroyImmediate/UnityEngine.Object lifecycle) rather than calling Cleanup.","Exclude Cleanup from any convention-based method invoker."],"exampleFix":"// before\neditor.Cleanup(); // InvalidOperationException\n\n// after\n// move disposal into OnDisable; do not call Cleanup","handlingStrategy":"validation","validationCode":"// Cleanup is intentionally non-functional; do not call it.\n// Dispose resources in OnDisable instead.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never call Cleanup; free resources in OnDisable.","For container teardown, destroy the editor object rather than calling Cleanup.","Keep Cleanup out of reflection-based disposal conventions."],"tags":["editor","lifecycle","api-misuse","disposal","internal"],"backgroundTag":null,"analyzedSha":"225b0fbdb57cc17d094e8056b71f8314aba56f73","analyzedAt":"2026-08-13T19:07:19.849Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}