Skip to content

Dispose XmlReaders in System.Xml.Xpath.XPathNavigator #103294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,7 @@ public virtual bool CheckValidity(XmlSchemaSet schemas, ValidationEventHandler v

CheckValidityHelper validityTracker = new CheckValidityHelper(validationEventHandler, reader);
validationEventHandler = new ValidationEventHandler(validityTracker.ValidationCallback);
XmlReader validatingReader = GetValidatingReader(reader, schemas, validationEventHandler, schemaType, schemaElement, schemaAttribute);
using XmlReader validatingReader = GetValidatingReader(reader, schemas, validationEventHandler, schemaType, schemaElement, schemaAttribute);

while (validatingReader.Read())
;
Expand Down Expand Up @@ -1291,7 +1291,7 @@ public virtual XmlWriter ReplaceRange(XPathNavigator lastSiblingToReplace)

public virtual void ReplaceSelf(string newNode)
{
XmlTextReader reader = CreateContextReader(newNode, false);
using XmlTextReader reader = CreateContextReader(newNode, false);
ReplaceSelf(reader);
}

Expand All @@ -1306,16 +1306,15 @@ public virtual void ReplaceSelf(XmlReader newNode)
{
throw new InvalidOperationException(SR.Xpn_BadPosition);
}
XmlWriter writer = ReplaceRange(this);
using XmlWriter writer = ReplaceRange(this);
BuildSubtree(newNode, writer);
writer.Close();
}

public virtual void ReplaceSelf(XPathNavigator newNode)
{
ArgumentNullException.ThrowIfNull(newNode);

XmlReader reader = newNode.CreateReader();
using XmlReader reader = newNode.CreateReader();
ReplaceSelf(reader);
}

Expand Down Expand Up @@ -1444,17 +1443,16 @@ public virtual string InnerXml

public virtual void AppendChild(string newChild)
{
XmlReader reader = CreateContextReader(newChild, true);
using XmlReader reader = CreateContextReader(newChild, true);
AppendChild(reader);
}

public virtual void AppendChild(XmlReader newChild)
{
ArgumentNullException.ThrowIfNull(newChild);

XmlWriter writer = AppendChild();
using XmlWriter writer = AppendChild();
BuildSubtree(newChild, writer);
writer.Close();
}

public virtual void AppendChild(XPathNavigator newChild)
Expand All @@ -1465,23 +1463,22 @@ public virtual void AppendChild(XPathNavigator newChild)
{
throw new InvalidOperationException(SR.Xpn_BadPosition);
}
XmlReader reader = newChild.CreateReader();
using XmlReader reader = newChild.CreateReader();
AppendChild(reader);
}

public virtual void PrependChild(string newChild)
{
XmlReader reader = CreateContextReader(newChild, true);
using XmlReader reader = CreateContextReader(newChild, true);
PrependChild(reader);
}

public virtual void PrependChild(XmlReader newChild)
{
ArgumentNullException.ThrowIfNull(newChild);

XmlWriter writer = PrependChild();
using XmlWriter writer = PrependChild();
BuildSubtree(newChild, writer);
writer.Close();
}

public virtual void PrependChild(XPathNavigator newChild)
Expand All @@ -1492,23 +1489,22 @@ public virtual void PrependChild(XPathNavigator newChild)
{
throw new InvalidOperationException(SR.Xpn_BadPosition);
}
XmlReader reader = newChild.CreateReader();
using XmlReader reader = newChild.CreateReader();
PrependChild(reader);
}

public virtual void InsertBefore(string newSibling)
{
XmlReader reader = CreateContextReader(newSibling, false);
using XmlReader reader = CreateContextReader(newSibling, false);
InsertBefore(reader);
}

public virtual void InsertBefore(XmlReader newSibling)
{
ArgumentNullException.ThrowIfNull(newSibling);

XmlWriter writer = InsertBefore();
using XmlWriter writer = InsertBefore();
BuildSubtree(newSibling, writer);
writer.Close();
}

public virtual void InsertBefore(XPathNavigator newSibling)
Expand All @@ -1519,23 +1515,22 @@ public virtual void InsertBefore(XPathNavigator newSibling)
{
throw new InvalidOperationException(SR.Xpn_BadPosition);
}
XmlReader reader = newSibling.CreateReader();
using XmlReader reader = newSibling.CreateReader();
InsertBefore(reader);
}

public virtual void InsertAfter(string newSibling)
{
XmlReader reader = CreateContextReader(newSibling, false);
using XmlReader reader = CreateContextReader(newSibling, false);
InsertAfter(reader);
}

public virtual void InsertAfter(XmlReader newSibling)
{
ArgumentNullException.ThrowIfNull(newSibling);

XmlWriter writer = InsertAfter();
using XmlWriter writer = InsertAfter();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are any of these changes actually improving things? It's possible they could lead to performance regressions, as these are mostly methods that could otherwise be inlined (in particular with dynamic PGO), and now with the exception handling they likely won't be. I'd prefer to close this PR unless it's actually solving a real run-time problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are any of these changes actually improving things?

I have no data to make that claim.

I'd prefer to close this PR unless it's actually solving a real run-time problem.

Fair enough!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, thanks

BuildSubtree(newSibling, writer);
writer.Close();
}

public virtual void InsertAfter(XPathNavigator newSibling)
Expand All @@ -1546,7 +1541,7 @@ public virtual void InsertAfter(XPathNavigator newSibling)
{
throw new InvalidOperationException(SR.Xpn_BadPosition);
}
XmlReader reader = newSibling.CreateReader();
using XmlReader reader = newSibling.CreateReader();
InsertAfter(reader);
}

Expand Down
Loading