unoplatform/uno · error · ObjectDisposedException

reader

Error message

reader

What it means

XamlXmlReader.Read throws ObjectDisposedException('reader') when Read is called after the reader was disposed (IsDisposed is true). The reader lazily initializes its node enumerator on first Read; once disposed, any further read attempt is rejected to prevent operating on released parser state.

Source

Thrown at src/SourceGenerators/System.Xaml/System.Xaml/XamlXmlReader.cs:217

			get { return iter != null ? iter.Current.NodeType : XamlNodeType.None; }
		}

		public override XamlSchemaContext SchemaContext {
			get { return parser.SchemaContext; }
		}

		public override XamlType Type {
			get { return iter != null && iter.Current.NodeType == XamlNodeType.StartObject ? (XamlType) iter.Current.NodeValue : null; }
		}

		public override object Value {
			get { return iter != null && iter.Current.NodeType == XamlNodeType.Value ? iter.Current.NodeValue : null; }
		}

		public override bool Read ()
		{
			if (IsDisposed)
				throw new ObjectDisposedException ("reader");
			if (iter == null)
				iter = parser.Parse ().GetEnumerator ();
			iter.MoveNext ();
			return iter.Current.NodeType != XamlNodeType.None;
		}
	}

	struct XamlXmlNodeInfo
	{
		public XamlXmlNodeInfo (XamlNodeType nodeType, object nodeValue, IXmlLineInfo lineInfo)
		{
			NodeType = nodeType;
			NodeValue = nodeValue;
			if (lineInfo != null && lineInfo.HasLineInfo ()) {
				HasLineInfo = true;
				LineNumber = lineInfo.LineNumber;
				LinePosition = lineInfo.LinePosition;
			} else {

View on GitHub (pinned to 0418340488)

Solutions

  1. Ensure the consumer finishes reading before the reader is disposed (move the using scope to wrap the entire consumption loop).
  2. Do not reuse a XamlXmlReader after disposal — create a fresh one for each parse session.
  3. For deferred/lazy iteration, materialize the nodes (e.g. via XamlServices.Transform into a XamlWriter) before disposing the reader.
  4. Track disposal state in your pipeline and stop issuing Read calls once disposed.

Example fix

// before
using (var reader = new XamlXmlReader(stream)) {
    return ReadLazy(reader); // caller iterates after using exits -> disposed
}

// after
XamlXmlReader Create() {
    // caller owns lifetime; do not dispose here
    return new XamlXmlReader(stream);
}
Defensive patterns

Strategy: validation

Validate before calling

if (reader.IsDisposed) throw new ObjectDisposedException(nameof(reader));
reader.Read();

Try / catch

try { while (reader.Read()) { /* consume */ } }
catch (ObjectDisposedException) { /* reader released upstream; stop iterating */ }

Prevention

When it happens

Trigger: Calling Read after the XamlXmlReader (or its underlying XmlReader/stream) was disposed — e.g. a using block disposed the reader but a deferred iterator or background task still calls Read.

Common situations: Streaming/lazy evaluation where the reader is disposed before the consumer finishes iterating; 'using' scope mismatch with async enumeration; code that caches a reader across disposal boundaries; reusing a reader after XamlServices.Transform closed resources.

Related errors


AI-assisted analysis of unoplatform/uno@0418340488 (2026-08-13). Data as JSON: /api/errors/1e4fa1cc6dddc9c4. Report an issue: GitHub.