{"record":{"id":"8aebcecfb641a810","repo":"dotnet/wpf","slug":"throw-new-argumentnullexception-nameof-section","errorCode":null,"errorMessage":"throw new ArgumentNullException(nameof(section));","messagePattern":"throw new ArgumentNullException\\(nameof\\(section\\)\\);","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs","lineNumber":106,"sourceCode":"        /// <param name=\"pixelHeight\"></param>\n        /// <param name=\"format\"></param>\n        /// <param name=\"stride\"></param>\n        /// <param name=\"offset\"></param>\n        /// <remarks>\n        ///     Callers must have UnmanagedCode permission to call this API.\n        /// </remarks>\n        public static unsafe BitmapSource CreateBitmapSourceFromMemorySection(\n            IntPtr section,\n            int pixelWidth,\n            int pixelHeight,\n            Media.PixelFormat format,\n            int stride,\n            int offset)\n        {\n\n            if (section == IntPtr.Zero)\n            {\n                throw new ArgumentNullException(nameof(section));\n            }\n\n            return new InteropBitmap(section, pixelWidth, pixelHeight, format, stride, offset);\n        }\n}\n}\n\n","sourceCodeStart":88,"sourceCodeEnd":114,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/Imaging.cs#L88-L114","documentation":"CreateBitmapSourceFromMemorySection throws ArgumentNullException when the win32 file-mapping handle passed as `section` is IntPtr.Zero. A WPF InteropBitmap must be backed by a real shared-memory section handle, so a null/zero handle cannot be used to create one.","triggerScenarios":"Calling Imaging.CreateBitmapSourceFromMemorySection with a handle that is IntPtr.Zero — typically because CreateFileMapping (or the memory-mapped-file SafeFileHandle) returned null/failed and the failure was never checked before passing the handle on.","commonSituations":"Interoperating with GDI/memory-mapped buffers shared with another process; the foreign process never created or closed the section, CreateFileMapping failed due to insufficient permissions or memory, or a NativeMemory/MemoryMappedFile handle was default-initialized instead of opened.","solutions":["Check the handle returned from CreateFileMapping/CreateDC/etc. for IntPtr.Zero or failure before passing it to CreateBitmapSourceFromMemorySection","Ensure the shared-memory section is actually created (correct size, valid PAGE_* protection, flProtect not 0)","If using System.IO.MemoryMappedFiles, open the MemoryMappedFile first and pass its SafeMemoryMappedFileHandle, verifying it is not IsInvalid/IsClosed","If the source may legitimately be absent, branch on IntPtr.Zero and skip/defer the bitmap creation instead of calling the API"],"exampleFix":"// before\nIntPtr section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, size, null);\nvar source = Imaging.CreateBitmapSourceFromMemorySection(section, w, h, PixelFormats.Pbgra32, stride, 0);\n// after\nIntPtr section = CreateFileMapping(new IntPtr(-1), IntPtr.Zero, 0x04, 0, size, null);\nif (section == IntPtr.Zero)\n    throw new Win32Exception(Marshal.GetLastWin32Error(), \"CreateFileMapping failed\");\nvar source = Imaging.CreateBitmapSourceFromMemorySection(section, w, h, PixelFormats.Pbgra32, stride, 0);","handlingStrategy":"validation","validationCode":"if (section == IntPtr.Zero) throw new InvalidOperationException(\"Shared-memory section handle not created\");\n// else safe to call Imaging.CreateBitmapSourceFromMemorySection(...)","typeGuard":"static bool IsValidSectionHandle(IntPtr h) => h != IntPtr.Zero;","tryCatchPattern":"try { var bmp = Imaging.CreateBitmapSourceFromMemorySection(section, w, h, fmt, stride, off); }\ncatch (ArgumentNullException ex) { /* handle missing section handle */ }","preventionTips":["Always check native CreateFileMapping return values and Marshal.GetLastWin32Error","Verify SafeMemoryMappedFileHandle.IsInvalid/IsClosed before interop","Create the section with a nonzero size and valid page protection"],"tags":["wpf","interop","null-argument","bitmap"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}