{"record":{"id":"6ff919ea0b4fe939","repo":"dotnet/wpf","slug":"argumentnullexception-buffer-sourcebuffer-was-intptr-zero","errorCode":null,"errorMessage":"ArgumentNullException (buffer/sourceBuffer was IntPtr.Zero)","messagePattern":"ArgumentNullException \\(buffer/sourceBuffer was IntPtr\\.Zero\\)","errorType":"validation","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs","lineNumber":833,"sourceCode":"            {\n                HRESULT.Check(MS.Win32.NativeMethods.E_INVALIDARG);\n            }\n            else if (destinationX < 0 || destinationY < 0)\n            {\n                HRESULT.Check((int)WinCodecErrors.WINCODEC_ERR_VALUEOVERFLOW);\n            }\n\n            //\n            // Sanitize the other parameters.\n            //\n            if (sourceBuffer == IntPtr.Zero)\n            {\n                // Backwards Compat:\n                //\n                // The original code would null-ref when it was passed a null\n                // buffer (IntPtr.Zero).  We choose to throw a better\n                // exception.\n                throw new ArgumentNullException(backwardsCompat ? \"buffer\" : \"sourceBuffer\");\n            }\n\n            Debug.Assert(!(backwardsCompat && sourceBufferStride < 1));\n            ArgumentOutOfRangeException.ThrowIfNegativeOrZero(sourceBufferStride);\n\n            if (sourceRect.Width == 0 || sourceRect.Height == 0)\n            {\n                Debug.Assert(!backwardsCompat);\n\n                // Nothing to do.\n                return;\n            }\n\n            checked\n            {\n                uint finalRowWidthInBits = (uint)((sourceRect.X + sourceRect.Width) * _format.InternalBitsPerPixel);\n                uint finalRowWidthInBytes = ((finalRowWidthInBits + 7) / 8);\n                uint requiredBufferSize = ((uint)(sourceRect.Y + sourceRect.Height - 1) * (uint)sourceBufferStride) + finalRowWidthInBytes;","sourceCodeStart":815,"sourceCodeEnd":851,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs#L815-L851","documentation":"The internal WritePixelsImpl receives the pixel buffer as either an Array or an IntPtr. Historically, IntPtr.Zero caused a null-reference crash deep in native code; the library now deliberately throws ArgumentNullException named 'buffer' (backwards-compat path) or 'sourceBuffer' when the pointer is IntPtr.Zero.","triggerScenarios":"Calling the IntPtr overload WritePixels(sourceRect, IntPtr buffer, ...) with IntPtr.Zero, or passing a pointer from an uninitialized/unallocated native buffer in backwardsCompat mode.","commonSituations":"Interop with native code where allocation failed silently and returned NULL; default-constructed IntPtr passed along; Marshal.AllocHGlobal result not checked.","solutions":["Check the IntPtr for Zero before calling and throw or return early.","Verify native allocation success (non-NULL) before passing pointers.","For the Array overloads, pass a non-null array."],"exampleFix":"// before\nIntPtr p = AllocBufferMaybe();\nbitmap.WritePixels(rect, p, stride, 0); // throws if p == IntPtr.Zero\n// after\nIntPtr p = AllocBufferMaybe();\nif (p == IntPtr.Zero) throw new OutOfMemoryException(\"buffer alloc failed\");\nbitmap.WritePixels(rect, p, stride, 0);","handlingStrategy":"type-guard","validationCode":"if (buffer == IntPtr.Zero) throw new ArgumentException(\"buffer must not be IntPtr.Zero\");","typeGuard":"bool IsValidNativeBuffer(IntPtr p) => p != IntPtr.Zero;","tryCatchPattern":"try { bitmap.WritePixels(rect, ptr, stride, 0); } catch (ArgumentNullException) { /* fix allocation */ }","preventionTips":["Always check native allocation results for NULL","Initialize IntPtr buffers before use","Prefer Array overloads when possible"],"tags":["wpf","bitmap","argument-null","interop"],"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"}