Delphi - TXMLDocument created at run-time generates AV, with component on the form is working

半世苍凉 提交于 2019-11-28 03:29:58

问题


I'm creating an instance of TXMLDocument at runtime, to load and parse a XML file. You can check the code below:

    unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, xmldom, XMLIntf, msxmldom, XMLDoc, StdCtrls;

type
  Txml = class(TForm)
//    XMLDocument1: TXMLDocument;
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  xml: Txml;

implementation

{$R *.dfm}

procedure Txml.FormCreate(Sender: TObject);
var    i,j:integer;
       aNode:IXMLNode;
       ws:String;
       XMLDocument1:TXMLDocument;
begin
 Memo1.Lines.Clear;
 XMLDocument1 := TXMLDocument.Create(nil);
 try
  XMLDocument1.LoadFromFile('C:\a.xml');
  XMLDocument1.Active := true;
  aNode := XMLDocument1.ChildNodes.First;
  while aNode<>nil do
  begin
   for i := 0 to aNode.ChildNodes.Count-1 do
    begin
     if aNode.ChildNodes[i].NodeName = 'Role' then
      begin
       Memo1.Lines.Add('Tag - '+aNode.ChildNodes[i].ChildNodes['Tag'].Text);
       for j := 0 to aNode.ChildNodes[i].ChildNodes.Count-1 do
        if aNode.ChildNodes[i].ChildNodes[j].HasChildNodes then
         begin
          ws :=  VarToStr(aNode.ChildNodes[i].ChildNodes[j].ChildValues['Tag']);
          if trim(ws)<>'' then
           Memo1.Lines.Add(ws);
          ws :=  VarToStr(aNode.ChildNodes[i].ChildNodes[j].ChildValues['Value']);
          if trim(ws)<>'' then
           Memo1.Lines.Add(ws);
         end;
      end;
    end;
   aNode := aNode.NextSibling;
  end;
  XMLDocument1.Active := false;
 finally
   FreeAndNil(XMLDocument1);
 end;
end;

end.

The problem is that this is generating an AV. As you probably have seen, before the component was on the form (// XMLDocument1: TXMLDocument;).

Why when the component was on the form the code was working, but when I'm creating it at run-time it generates AV?

LE: solution: based on the answers/comments and Delphi Help:

XMLDocument1 : IXMLDocument;  //not TXMLDocument

XMLDocument1 := LoadXMLDocument(...);

FreeAndNil;// must be deleted

回答1:


From what I know you should be using interface IDoc: IXMLDocument; instead.

From docs:

When TXMLDocument is created without an Owner, it behaves like an interfaced object. That is, when all references to its interface are released, the TXMLDocument instance is automatically freed. When TXMLDocument is created with an Owner, however, it behaves like any other component, and is freed by its Owner.

In other words, when creating a TXMLDocument instance with a nil Owner, do not call Free() or FreeAndNil() on the instance, and you must assign the object to an IXMLDocument variable so its now-active reference count is managed properly.




回答2:


You need to provide an Owner to TXMLDocument when creating it in run-time.

XMLDocument1 := TXMLDocument.Create(xml);


来源:https://stackoverflow.com/questions/8398703/delphi-txmldocument-created-at-run-time-generates-av-with-component-on-the-fo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!