{"record":{"id":"92f5db54daee3d5e","repo":"AvaloniaUI/Avalonia","slug":"drawingcontext","errorCode":null,"errorMessage":"DrawingContext","messagePattern":"DrawingContext","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Media/ImmediateDrawingContext.cs","lineNumber":241,"sourceCode":"\n            internal PushedState(ImmediateDrawingContext context, PushedStateType type, Matrix matrix = default)\n            {\n                if (context._states is null)\n                    throw new ObjectDisposedException(nameof(ImmediateDrawingContext));\n\n                _context = context;\n                _type = type;\n                _matrix = matrix;\n                _level = context._currentLevel += 1;\n                context._states.Push(this);\n            }\n\n            public void Dispose()\n            {\n                if (_type == PushedStateType.None)\n                    return;\n                if (_context._states is null || _context._transformContainers is null)\n                    throw new ObjectDisposedException(nameof(DrawingContext));\n                if (_context._currentLevel != _level)\n                    throw new InvalidOperationException(\"Wrong Push/Pop state order\");\n                _context._currentLevel--;\n                _context._states.Pop();\n                if (_type == PushedStateType.Matrix)\n                    _context.CurrentTransform = _matrix;\n                else if (_type == PushedStateType.Clip)\n                    _context.PlatformImpl.PopClip();\n                else if (_type == PushedStateType.Opacity)\n                    _context.PlatformImpl.PopOpacity();\n                else if (_type == PushedStateType.GeometryClip)\n                    _context.PlatformImpl.PopGeometryClip();\n                else if (_type == PushedStateType.OpacityMask)\n                    _context.PlatformImpl.PopOpacityMask();\n                else if (_type == PushedStateType.MatrixContainer)\n                {\n                    var cont = _context._transformContainers.Pop();\n                    _context._currentContainerTransform = cont.ContainerTransform;","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Media/ImmediateDrawingContext.cs#L223-L259","documentation":"PushedState.Dispose throws ObjectDisposedException(nameof(DrawingContext)) when _states or _transformContainers is null — i.e. disposing a PushedState whose owning context was already disposed. This is the use-after-dispose guard for the per-push disposable.","triggerScenarios":"A PushedState disposable outliving its ImmediateDrawingContext: the context's using-block exits (disposing all states), then a previously returned PushedState is disposed again, or a leaked PushedState is disposed after the context was torn down.","commonSituations":"Storing PushedState structs in fields/closures and disposing them after the render context closed; nested using scopes that leak.","solutions":["Dispose PushedState values within the context's lifetime (they are returned by using declarations).","Do not retain PushedState structs beyond the draw call.","Ensure the context outlives every push/pop pair."],"exampleFix":"// before - leaked state disposed after context gone\nPushedState s;\nusing (var ctx = ...CreateDrawingContext()) { s = ctx.PushTransform(m); }\ns.Dispose(); // ObjectDisposedException\n\n// after\nusing (var ctx = ...CreateDrawingContext())\nusing (ctx.PushTransform(m)) { /* draw */ }","handlingStrategy":"validation","validationCode":"// Do not dispose a PushedState after its owning context is disposed.\nusing (var ctx = CreateContext())\nusing (var s = ctx.PushTransform(m))\n{ /* draw */ } // s disposed here, within ctx lifetime","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Dispose PushedState values within the context lifetime using using-declarations.","Never retain PushedState structs beyond the draw call.","Keep push/pop lexically nested."],"tags":["rendering","drawingcontext","objectdisposed","lifecycle"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}