Unity-Technologies/UnityCsReference · error · InvalidOperationException
You shouldn't call Cleanup for Editors. Dispose resources in
Error message
You shouldn't call Cleanup for Editors. Dispose resources in Editor.OnDisable
What it means
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.
Source
Thrown at Editor/Mono/Inspector/Editor.cs:1516
return true;
}
public virtual bool UseDefaultMargins()
{
return true;
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void Initialize(UnityObject[] targets)
{
throw new InvalidOperationException("You shouldn't call Initialize for Editors");
}
[EditorBrowsable(EditorBrowsableState.Never)]
public void Cleanup()
{
throw new InvalidOperationException("You shouldn't call Cleanup for Editors. Dispose resources in " +
"Editor.OnDisable");
}
public bool MoveNextTarget()
{
referenceTargetIndex++;
return referenceTargetIndex < targets.Length;
}
public void ResetTarget()
{
referenceTargetIndex = 0;
}
// Implement this method to show a limited inspector for showing tweakable parameters in an Asset Store preview.
internal virtual void OnAssetStoreInspectorGUI()
{
}View on GitHub (pinned to 225b0fbdb5)
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.
Example fix
// before editor.Cleanup(); // InvalidOperationException // after // move disposal into OnDisable; do not call Cleanup
Defensive patterns
Strategy: validation
Validate before calling
// Cleanup is intentionally non-functional; do not call it. // Dispose resources in OnDisable instead.
Prevention
- 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.
When it happens
Trigger: Explicitly calling editor.Cleanup(), or an auto-disposal/reflection routine that invokes public methods matching a cleanup convention.
Common situations: 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.
Related errors
- You shouldn't call Initialize for Editors
- gameObject
- Delegate already registered for dropDestinationId:{dropDstId
- You can't set the target on an editor.
- Value for parameter atlases is null
AI-assisted analysis of Unity-Technologies/UnityCsReference@225b0fbdb5 (2026-08-13).
Data as JSON: /api/errors/599374add562dd10.
Report an issue: GitHub.