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

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

By Zarko Gajic, About.com

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

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. 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.