{"record":{"id":"f44eb3da50af1b2e","repo":"HandyOrg/HandyControl","slug":"statusexception-interopmethods-gdip-outofmemory","errorCode":null,"errorMessage":"StatusException(InteropMethods.Gdip.OutOfMemory)","messagePattern":"StatusException\\(InteropMethods\\.Gdip\\.OutOfMemory\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Shared/HandyControl_Shared/Controls/Image/GifImage.cs","lineNumber":366,"sourceCode":"        {\n            var status = InteropMethods.Gdip.GdipImageGetFrameDimensionsCount(new HandleRef(this, NativeImage), out var count);\n\n            if (status != InteropMethods.Gdip.Ok)\n            {\n                throw InteropMethods.Gdip.StatusException(status);\n            }\n\n            if (count <= 0)\n            {\n                return new Guid[0];\n            }\n\n            var size = Marshal.SizeOf(typeof(Guid));\n\n            var buffer = Marshal.AllocHGlobal(checked(size * count));\n            if (buffer == IntPtr.Zero)\n            {\n                throw InteropMethods.Gdip.StatusException(InteropMethods.Gdip.OutOfMemory);\n            }\n\n            status = InteropMethods.Gdip.GdipImageGetFrameDimensionsList(new HandleRef(this, NativeImage), buffer, count);\n\n            if (status != InteropMethods.Gdip.Ok)\n            {\n                Marshal.FreeHGlobal(buffer);\n                throw InteropMethods.Gdip.StatusException(status);\n            }\n\n            var guids = new Guid[count];\n\n            try\n            {\n                for (var i = 0; i < count; i++)\n                {\n                    guids[i] = (Guid) InteropMethods.PtrToStructure((IntPtr) ((long) buffer + size * i), typeof(Guid));\n                }","sourceCodeStart":348,"sourceCodeEnd":384,"githubUrl":"https://github.com/HandyOrg/HandyControl/blob/2c0875ebd67326e0c67282967e3e809c69282fee/src/Shared/HandyControl_Shared/Controls/Image/GifImage.cs#L348-L384","documentation":"This is a GDI+ StatusException wrapping the OutOfMemory status code, thrown when Marshal.AllocHGlobal fails to allocate unmanaged memory for the frame-dimension GUID buffer. The library throws it because the native allocation returned IntPtr.Zero and proceeding would corrupt the native call. Note: GDI+'s OutOfMemory status often means a generic internal failure, not literally insufficient RAM.","triggerScenarios":"Calling into GifImage frame-dimension enumeration (e.g. during initialization or EnsureSave/CanAnimate paths) when AllocHGlobal(checked(size*count)) cannot secure the requested unmanaged block — typically a huge or corrupt frame-dimension count read from a malformed GIF.","commonSituations":"Corrupt or truncated GIF files that report an absurd frame dimension count; memory pressure in the process (32-bit address space exhaustion); running after the native GDI+ handle has gone bad.","solutions":["Validate the GIF file loads correctly (e.g. open it with a known-good decoder) before feeding it to GifImage","Catch OutOfMemoryException/StatusException around GifImage construction and fall back to a static frame","Run the process as 64-bit or reduce concurrent image allocations to relieve address-space pressure","Dispose GifImage promptly (it holds a native handle) to avoid leaked unmanaged memory"],"exampleFix":"// before\nvar gif = new GifImage(stream);\n// after\nGifImage gif = null;\ntry { gif = new GifImage(stream); }\ncatch (Exception ex) when (ex is OutOfMemoryException || ex.Source != null && ex.Message.Contains(\"OutOfMemory\"))\n{ /* fall back to static rendering */ }","handlingStrategy":"try-catch","validationCode":"if (buffer == IntPtr.Zero || count <= 0) { /* skip native call, use fallback */ }","typeGuard":"static bool IsUsableGifStream(Stream s) => s != null && s.CanRead && s.Length > 6;","tryCatchPattern":"try { var gif = new GifImage(stream); }\ncatch (OutOfMemoryException) { /* static fallback frame */ }","preventionTips":["Validate GIF magic bytes before loading","Dispose GifImage deterministically to free native memory","Prefer 64-bit processes when handling many images"],"tags":["gdiplus","interop","memory","image"],"backgroundTag":"gdiplus-out-of-memory","analyzedSha":"2c0875ebd67326e0c67282967e3e809c69282fee","analyzedAt":"2026-09-14T14:45:29.754Z","contentChangedAt":"2026-09-14T14:45:29.754Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}