dotnet/wpf · error · NullReferenceException

new NullReferenceException()

Error message

new NullReferenceException()

What it means

OpenProfileStream throws NullReferenceException when the internal native color-context handle (_colorContextHelper) is invalid. Per the source comment, this simulates 3.x behavior: the context was constructed from a corrupt or unreadable profile (bad handle from ColorContext(SafeMILHandle) or FromRawBytes), so it behaves like a null object.

Solutions

  1. Validate the source profile bytes/URI before constructing the ColorContext; ensure the image or package is not truncated.
  2. Check that the profile data came from a trusted, complete source; re-acquire the image/file and rebuild the context.
  3. Wrap OpenProfileStream in try/catch for NullReferenceException and fall back to constructing a fresh ColorContext from a known-good profile URI.

Example fix

// before
using var s = suspiciousContext.OpenProfileStream(); // NullReferenceException if handle invalid
// after
Stream s;
try { s = suspiciousContext.OpenProfileStream(); }
catch (NullReferenceException) { s = new ColorContext(new Uri("pack://application:,,,/Profiles/sRGB.icc", UriKind.Absolute)).OpenProfileStream(); }
Defensive patterns

Strategy: try-catch

Validate before calling

if (context == null || context.ProfileUri == null && context.ToString() == string.Empty) { /* treat as invalid; rebuild from known-good profile */ }

Type guard

bool IsUsable(ColorContext c) { try { using var _ = c.OpenProfileStream(); return true; } catch (NullReferenceException) { return false; } }

Try / catch

try { using var s = ctx.OpenProfileStream(); Process(s); }
catch (NullReferenceException) { Log.Warning("Corrupt embedded color profile"); RebuildContextFromDefaultProfile(); }

Prevention

When it happens

Trigger: Calling colorContext.OpenProfileStream() on a ColorContext created via ColorContext(SafeMILHandle) or FromRawBytes from corrupt/truncated ICC profile bytes, where the early-exit paths left an invalid handle.

Common situations: Reading embedded profiles from damaged/corrupted images or XPS packages; parsing raw ICC bytes downloaded incompletely or truncated by a bad stream.

Understand the failure class

Background: "failed to read file", EACCES, ENOENT and "could not read <path>" errors: when a program can't read a file from disk — this error's family across 49 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ColorContext.cs:215

        #endregion

        #region Public Methods

        /// <summary>
        /// Returns a memory stream to the color profile bits
        /// </summary>
        public Stream OpenProfileStream()
        {
            //
            // 3.* backwards compat for a "bad" ColorContext. Now the helper is a
            // struct so when it's invalid we'll pretend it's a reference type. This
            // should only happen if the color profile is corrupt (see early exits in
            // ColorContext(SafeMILHandle) and FromRawBytes()).
            //
            if (_colorContextHelper.IsInvalid)
            {
                throw new NullReferenceException();
            }
            
            uint profileSize = 0;
            _colorContextHelper.GetColorProfileFromHandle(null, ref profileSize);
            
            byte[] profile = new byte[profileSize];
            _colorContextHelper.GetColorProfileFromHandle(profile, ref profileSize);

            return new MemoryStream(profile);
        }

        #endregion Public Methods

        #region Public Properties

        /// <summary>
        /// ProfileUri
        /// </summary>

View on GitHub (pinned to 81131a70a4)