dotnet/wpf · error · NullReferenceException
Buffer address passed to GetText cannot be NULL.
Error message
Buffer address passed to GetText cannot be NULL.
What it means
IFilter.GetText rejects a null (IntPtr.Zero) buffer before attempting to write chunk text into unmanaged memory. The COM/IFilter contract requires the caller (the index service) to supply a valid pre-allocated character buffer sized by bufCharacterCount; a zero pointer means the caller violated that contract, so the filter throws NullReferenceException rather than corrupting memory via Marshal writes.
Solutions
- Allocate a valid buffer (Marshal.AllocHGlobal) sized bufCharacterCount * sizeof(char) before calling GetText
- Check buffer allocation success before calling GetText
- If the count is zero GetText is a no-op, but never pass IntPtr.Zero with a nonzero count
Example fix
// before
filter.GetText(ref count, IntPtr.Zero);
// after
IntPtr buffer = Marshal.AllocHGlobal((int)(count * 2));
try { filter.GetText(ref count, buffer); }
finally { Marshal.FreeHGlobal(buffer); } Defensive patterns
Strategy: validation
Validate before calling
if (pBuffer == IntPtr.Zero && bufCharacterCount > 0)
throw new ArgumentException("GetText buffer must be allocated", nameof(pBuffer)); Try / catch
try { filter.GetText(ref count, buffer); }
catch (NullReferenceException) { /* reallocate buffer and retry */ } Prevention
- Always allocate unmanaged buffers before interop calls
- Never use IntPtr.Zero to probe required buffer sizes
- Free buffers in finally blocks to avoid cascading allocation failures
When it happens
Trigger: Calling IFilter.GetText with pBuffer == IntPtr.Zero and a non-zero bufCharacterCount.
Common situations: Interop callers whose Marshal.AllocHGlobal allocation failed or returned IntPtr.Zero, or callers mistakenly passing IntPtr.Zero to probe required buffer size.
Related errors
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/420f72b41edbf454.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/IO/Packaging/XpsFilter.cs:111
/// <summary>
/// Gets text content corresponding to current chunk.
/// </summary>
/// <param name="bufCharacterCount">size of buffer in characters</param>
/// <param name="pBuffer">buffer pointer</param>
/// <remarks>Supported for indexing content of Package.</remarks>
void IFilter.GetText(ref uint bufCharacterCount, IntPtr pBuffer)
{
if (_filter == null)
{
throw new COMException(SR.FileToFilterNotLoaded,
(int)FilterErrorCode.FILTER_E_ACCESS);
}
// NULL is not an acceptable value for pBuffer
if (pBuffer == IntPtr.Zero)
{
throw new NullReferenceException(SR.FilterNullGetTextBufferPointer);
}
// If there is 0 byte to write, this is a no-op.
if (bufCharacterCount == 0)
{
return;
}
// Because we should always return the string with null terminator, a buffer size
// of one character can hold the null terminator only, we can always write the
// terminator to the buffer and return directly.
if (bufCharacterCount == 1)
{
Marshal.WriteInt16(pBuffer, 0);
return;
}
// Record the original buffer size. bufCharacterCount may be changed later.View on GitHub (pinned to 81131a70a4)