dotnet/wpf · error · ArgumentNullException

ArgumentNullException("characterBufferReference.CharacterBuf…

Error message

ArgumentNullException("characterBufferReference.CharacterBuffer")

What it means

TextCharacters.GetTextShapeableSymbols validates that the supplied characterBufferReference.CharacterBuffer is non-null and throws ArgumentNullException("characterBufferReference.CharacterBuffer"). The method maps characters to glyph runs and cannot operate without an underlying character buffer.

Solutions

  1. Ensure the CharacterBufferReference passed in is constructed over a real buffer: new CharacterBufferReference(charArray, offset, count) or via TextStore
  2. Fix the TextSource.GetTextRun implementation so the TextCharacters run itself was created with a valid buffer
  3. Null-check characterBufferReference.CharacterBuffer before calling and rebuild the run if null
  4. If the source string is empty/null, return a TextEndOfLine/empty run instead of a buffer reference over null

Example fix

// before
var reference = new CharacterBufferReference(null, 0); // null buffer
run.GetTextShapeableSymbols(props, reference, ...);
// after
if (text == null) text = string.Empty;
var reference = new CharacterBufferReference(text.ToCharArray(), 0);
run.GetTextShapeableSymbols(props, reference, ...);
Defensive patterns

Strategy: type-guard

Validate before calling

if (characterBufferReference.CharacterBuffer == null)
    throw new InvalidOperationException("Character buffer not initialized");

Type guard

bool HasBuffer(CharacterBufferReference r) => r.CharacterBuffer != null;

Try / catch

try { run.GetTextShapeableSymbols(props, reference, ...); }
catch (ArgumentNullException ex) { log.Error("Null character buffer in run", ex); throw; }

Prevention

When it happens

Trigger: Calling GetTextShapeableSymbols with a CharacterBufferReference whose CharacterBuffer is null — e.g. default-constructed CharacterBufferReference, or one built from a null string/array/char* source.

Common situations: Custom text source returning a CharacterBufferReference over a null string; run sliced/copied incorrectly so its buffer reference was never initialized; interop path passing a null char buffer.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/482f9b2208a6c5b5. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextCharacters.cs:186

        /// Shape items are delimited by 
        ///     Change of writing system
        ///     Change of glyph typeface
        /// </summary>
        IList<TextShapeableSymbols> ITextSymbols.GetTextShapeableSymbols(
            GlyphingCache               glyphingCache,
            CharacterBufferReference    characterBufferReference,
            int                         length,
            bool                        rightToLeft,
            bool                        isRightToLeftParagraph,
            CultureInfo                 digitCulture,
            TextModifierScope           textModifierScope,
            TextFormattingMode          textFormattingMode,
            bool                        isSideways
            )
        {
            if (characterBufferReference.CharacterBuffer == null)
            {
                throw new ArgumentNullException("characterBufferReference.CharacterBuffer");
            }
            
            int offsetToFirstChar = characterBufferReference.OffsetToFirstChar - _characterBufferReference.OffsetToFirstChar;

            Debug.Assert(characterBufferReference.CharacterBuffer == _characterBufferReference.CharacterBuffer);
            Debug.Assert(offsetToFirstChar >= 0 && offsetToFirstChar < _length);

            if (    length < 0 
                ||  offsetToFirstChar + length > _length)
            {
                length = _length - offsetToFirstChar;
            }

            // Get the actual text run properties in effect, after invoking any
            // text modifiers that may be in scope.
            TextRunProperties textRunProperties = _textRunProperties;
            if (textModifierScope != null)
            {

View on GitHub (pinned to 81131a70a4)