{"record":{"id":"e88c1c837fe9aa7f","repo":"stride3d/stride","slug":"pointer-cannot-be-equal-to-intptr-zero","errorCode":null,"errorMessage":"Pointer cannot be equal to IntPtr.Zero","messagePattern":"Pointer cannot be equal to IntPtr\\.Zero","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/engine/Stride.Foundation/Graphics/PixelBuffer.cs","lineNumber":66,"sourceCode":"\n        /// <summary>\n        /// True when RowStride == sizeof(pixelformat) * width\n        /// </summary>\n        private readonly bool isStrictRowStride;\n\n        /// <summary>\n        /// Initializes a new instance of the <see cref=\"PixelBuffer\" /> struct.\n        /// </summary>\n        /// <param name=\"width\">The width.</param>\n        /// <param name=\"height\">The height.</param>\n        /// <param name=\"format\">The format.</param>\n        /// <param name=\"rowStride\">The row pitch.</param>\n        /// <param name=\"bufferStride\">The slice pitch.</param>\n        /// <param name=\"dataPointer\">The pixels.</param>\n        public PixelBuffer(int width, int height, PixelFormat format, int rowStride, int bufferStride, IntPtr dataPointer)\n        {\n            if (dataPointer == IntPtr.Zero)\n                throw new ArgumentException(\"Pointer cannot be equal to IntPtr.Zero\", \"dataPointer\");\n\n            this.width = width;\n            this.height = height;\n            this.format = format;\n            this.rowStride = rowStride;\n            this.bufferStride = bufferStride;\n            this.dataPointer = dataPointer;\n            this.pixelSize = format.SizeInBytes;\n            this.isStrictRowStride = (pixelSize * width) == rowStride;\n        }\n\n        /// <summary>\n        /// Gets the width.\n        /// </summary>\n        /// <value>The width.</value>\n        public int Width { get { return width; } }\n\n        /// <summary>","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/engine/Stride.Foundation/Graphics/PixelBuffer.cs#L48-L84","documentation":"The PixelBuffer constructor requires a non-null native pointer because the buffer is a view over existing pixel memory, never an owner. Passing IntPtr.Zero would produce a buffer whose DataPointer is invalid, so the constructor throws ArgumentException for the dataPointer argument.","triggerScenarios":"Calling new PixelBuffer(width, height, format, rowStride, bufferStride, dataPointer) with dataPointer == IntPtr.Zero, typically when a native allocation (e.g. from a GPU staging readback or Marshal.AllocHGlobal) returned zero or was not yet initialized.","commonSituations":"Interop code where a native allocation failed and returned IntPtr.Zero; calling the constructor before a staging texture readback populated the pointer; mis-ordered init code where the pointer field is still default(IntPtr).","solutions":["Check the pointer for IntPtr.Zero before constructing and handle the failed allocation.","Allocate memory with Marshal.AllocHGlobal or obtain a valid pointer from a staging resource before wrapping it.","Use an Image/PixelBuffer factory that allocates internally instead of wrapping raw memory."],"exampleFix":"// before\nvar pb = new PixelBuffer(w, h, format, rowStride, bufferStride, ptr);\n// after\nif (ptr == IntPtr.Zero) throw new OutOfMemoryException(\"pixel allocation failed\");\nvar pb = new PixelBuffer(w, h, format, rowStride, bufferStride, ptr);","handlingStrategy":"validation","validationCode":"if (dataPointer == IntPtr.Zero)\n    throw new InvalidOperationException(\"Cannot wrap pixel data: native allocation returned zero\");","typeGuard":"bool HasValidPointer(IntPtr p) => p != IntPtr.Zero;","tryCatchPattern":"try { var pb = new PixelBuffer(w, h, fmt, rowStride, bufferStride, ptr); }\ncatch (ArgumentException ex) when (ex.ParamName == \"dataPointer\") { log.Error(\"null pixel data pointer\"); throw; }","preventionTips":["Always check the result of native allocations for IntPtr.Zero before wrapping.","Wrap pointer creation and PixelBuffer construction in the same helper.","Prefer factory methods (Image.New) that own allocation over raw-pointer constructors."],"tags":["graphics","pixel-buffer","null-pointer","interop"],"backgroundTag":"null-argument","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}