1. Technology

How to create, use and free TXMLDocument dynamically (without an AV)

By

If you need to work with dynamically created TXMLDocument component, you might be getting access violations once you try to Free the object.

If TXMLDocument is used as a component, then it's lifetime is managed by it's owner (a form you dropped the component on), if it is created with no owner, then its lifetime is manged through the IXMLDocument interface. When dynamically creating a TXMLDocument you should always assign the result to an IXMLDocument interface.

Being an interface, it'll be freed automatically. If it is a local variable, it'll get freed when the method returns. You can explicitly free it by saying XMLDoc := nil;

~~~~~~~~~~~~~~~~~~~~~~~~~
XMLDoc := TXMLDocument.Create(nil) ;
try
  XMLDoc.Active := True;
  //use XMLDoc here
  XMLDoc.Active := False;
finally
XMLDoc := nil;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Timeout MessageBox
« How to locate and highlight a string in a TWebBrowser

©2014 About.com. All rights reserved.