{"record":{"id":"c0f03238c3acd112","repo":"AvaloniaUI/Avalonia","slug":"compositionimportedgpuimage","errorCode":null,"errorMessage":"CompositionImportedGpuImage","messagePattern":"CompositionImportedGpuImage","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"src/Avalonia.Base/Rendering/Composition/CompositionInterop.cs","lineNumber":124,"sourceCode":"        IExternalObjectsRenderInterfaceContextFeature feature,\n        Func<IPlatformRenderInterfaceImportedImage> importer, IPlatformHandle? handle): base(compositor, context, feature, handle)\n    {\n        _importer = importer;\n    }\n\n    protected override void Import()\n    {\n        using (Compositor.Server.RenderInterface.EnsureCurrent())\n        {\n            // The original context was lost and the new one might have different capabilities\n            if (Context != Compositor.Server.RenderInterface.Value)\n                throw new PlatformGraphicsContextLostException();\n            _image = _importer();\n        }\n    }\n\n    public IPlatformRenderInterfaceImportedImage Image =>\n        _image ?? throw new ObjectDisposedException(nameof(CompositionImportedGpuImage));\n\n    public bool IsUsable => _image != null && Compositor.Server.RenderInterface.Value == Context;\n\n    public override void Dispose()\n    {\n        _image?.Dispose();\n        _image = null!;\n    }\n}\n\nclass CompositionImportedGpuSemaphore : CompositionGpuImportedObjectBase, ICompositionImportedGpuSemaphore\n{\n    private readonly IPlatformHandle _handle;\n    private IPlatformRenderInterfaceImportedSemaphore? _semaphore;\n\n    public CompositionImportedGpuSemaphore(IPlatformHandle handle,\n        Compositor compositor, IPlatformRenderInterfaceContext context,\n        IExternalObjectsRenderInterfaceContextFeature feature) : base(compositor, context, feature, handle)","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/AvaloniaUI/Avalonia/blob/11c542726898ae954a1ef668c65ec79ec92ab17d/src/Avalonia.Base/Rendering/Composition/CompositionInterop.cs#L106-L142","documentation":"Thrown as ObjectDisposedException (nameof CompositionImportedGpuImage) by the Image property when the imported GPU image is null — which happens after Dispose() nulls it, or if Import() was never successful. The property is the accessor for the platform handle; once disposed it is unusable.","triggerScenarios":"Accessing importedGpuImage.Image after calling Dispose(), or after a failed Import (e.g. context lost). IsUsable should be checked first: it returns true only when _image is non-null and the render interface context matches.","commonSituations":"Holding a reference to an imported image across a device-loss/context-reset; accessing Image after the compositor disposed the object; racing disposal with a render read.","solutions":["Check IsUsable before reading Image, and re-import if false.","Do not access Image after Dispose(); drop the reference.","Handle PlatformGraphicsContextLostException during import and recreate the resource on the new context."],"exampleFix":"// before\nvar img = importedGpuImage.Image; // throws after dispose / context loss\n// after\nif (!importedGpuImage.IsUsable) importedGpuImage = Reimport(compositor);\nvar img = importedGpuImage.Image;","handlingStrategy":"validation","validationCode":"if (!importedGpuImage.IsUsable) importedGpuImage = Reimport(compositor);\nvar img = importedGpuImage.Image;","typeGuard":"static bool IsUsable(CompositionImportedGpuImage i) => i.IsUsable;","tryCatchPattern":"try { use(importedGpuImage.Image); }\ncatch (ObjectDisposedException) { importedGpuImage = Reimport(compositor); use(importedGpuImage.Image); }","preventionTips":["Always check IsUsable before Image and re-import when false.","Handle PlatformGraphicsContextLostException on import for device-loss recovery.","Drop references after Dispose() and stop rendering the resource."],"tags":["rendering","composition","gpu","object-disposed","interop"],"backgroundTag":null,"analyzedSha":"11c542726898ae954a1ef668c65ec79ec92ab17d","analyzedAt":"2026-08-13T11:57:40.261Z","schemaVersion":2},"datasetVersion":"2026-08-13T14:17:21.547Z"}