1. Home
  2. Computing & Technology
  3. Delphi Programming

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

By , About.com Guide

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

Explore Delphi Programming
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2004 Delphi Tips
  7. How to create, use and free TXMLDocument dynamically (without an AV)

©2009 About.com, a part of The New York Times Company.

All rights reserved.