{"record":{"id":"f98dae707a6ca43c","repo":"dotnet/machinelearning","slug":"object-is-disposed","errorCode":null,"errorMessage":"Object is disposed.","messagePattern":"Object is disposed\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.ImageAnalytics/MLImage.cs","lineNumber":452,"sourceCode":"            { \".jpg\", SKEncodedImageFormat.Jpeg },\n            { \".jpeg\", SKEncodedImageFormat.Jpeg },\n            { \".gif\", SKEncodedImageFormat.Gif },\n            { \".ico\", SKEncodedImageFormat.Ico },\n            { \".astc\", SKEncodedImageFormat.Astc },\n            { \".avif\", SKEncodedImageFormat.Avif },\n            { \".dng\", SKEncodedImageFormat.Dng },\n            { \".heif\", SKEncodedImageFormat.Heif },\n            { \".ktx\", SKEncodedImageFormat.Ktx },\n            { \".pkm\", SKEncodedImageFormat.Pkm },\n            { \".wbmp\", SKEncodedImageFormat.Wbmp },\n            { \".webp\", SKEncodedImageFormat.Webp }\n        };\n\n        private void ThrowInvalidOperationExceptionIfDisposed()\n        {\n            if (_image is null)\n            {\n                throw new InvalidOperationException(\"Object is disposed.\");\n            }\n        }\n    }\n\n    /// <summary>\n    /// Specifies the format of the color data for each pixel in the image.\n    /// </summary>\n    public enum MLPixelFormat\n    {\n        /// <summary>\n        /// The pixel format is unknown.\n        /// </summary>\n        Unknown,\n\n        /// <summary>\n        /// Specifies that the format is 32 bits per pixel; 8 bits each are used for the blue, green, red, and alpha components.\n        /// The color components are stored in blue, green, red, and alpha order\n        /// </summary>","sourceCodeStart":434,"sourceCodeEnd":470,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.ImageAnalytics/MLImage.cs#L434-L470","documentation":"MLImage wraps a disposable SKBitmap; ThrowInvalidOperationExceptionIfDisposed is called at the start of most members (PixelFormat, GetBGRPixels, Pixels, Tag, Width, Height, Save, etc.). Once Dispose ran, _image is null and any member access throws 'Object is disposed.'","triggerScenarios":"Accessing any property or method on an MLImage after calling Dispose() — including Width, Height, Pixels, Tag, PixelFormat, GetBGRPixels, or Save.","commonSituations":"Using images inside ML.NET pipelines after an owning using-block disposed them; double-dispose followed by access; caching MLImage instances across requests while disposal happens concurrently.","solutions":["Do not call members after Dispose; keep the image alive for its entire usage scope","Re-load the image (MLImage.CreateFromFile) if it was disposed prematurely","Wrap access in try-catch for InvalidOperationException and treat as a lifecycle bug","Check ObjectDisposed-like state via try/catch since MLImage exposes no IsDisposed property"],"exampleFix":"// before\nusing var image = MLImage.CreateFromFile(path);\nDoWork(image);\n// disposed here, then:\nvar w = image.Width; // throws\n// after\nMLImage image = MLImage.CreateFromFile(path);\nvar w = image.Width;\nDoWork(image);\nimage.Dispose();","handlingStrategy":"try-catch","validationCode":"// no public IsDisposed; track ownership yourself\nbool usable = image != null && !_ownsAndDisposedSet.Contains(image);","typeGuard":"static bool IsUsable(MLImage img) => img is not null; // track Dispose calls in your own wrapper","tryCatchPattern":"try { var w = image.Width; } catch (InvalidOperationException ex) when (ex.Message == \"Object is disposed.\") { image = MLImage.CreateFromFile(path); }","preventionTips":["Keep MLImage lifetime and usage in the same scope (avoid using-blocks when later access is needed)","Never access members after Dispose; set your reference to null on dispose","Avoid sharing MLImage instances across threads without ownership rules","Re-create images from the source file rather than caching disposed instances"],"tags":["csharp","image","disposed","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}