简体   繁体   English

Delphi - 在运行时创建的TXMLDocument生成AV,表单上的组件正在工作

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

I'm creating an instance of TXMLDocument at runtime, to load and parse a XML file. 我正在运行时创建一个TXMLDocument实例,以加载和解析XML文件。 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. 问题是这会产生AV。 As you probably have seen, before the component was on the form (// XMLDocument1: TXMLDocument;). 正如您可能已经看到的那样,组件在表单上之前(// 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? 为什么当组件在代码工作的表单上时,但是当我在运行时创建它时它会生成AV?

LE: solution : based on the answers/comments and Delphi Help: LE: 解决方案 :基于答案/评论和Delphi帮助:

XMLDocument1 : IXMLDocument;  //not TXMLDocument

XMLDocument1 := LoadXMLDocument(...);

FreeAndNil;// must be deleted

From what I know you should be using interface IDoc: IXMLDocument; 据我所知,你应该使用接口IDoc: IXMLDocument; instead. 代替。

From docs: 来自docs:

When TXMLDocument is created without an Owner, it behaves like an interfaced object. 在没有所有者的情况下创建TXMLDocument时,它的行为类似于接口对象。 That is, when all references to its interface are released, the TXMLDocument instance is automatically freed. 也就是说,当释放对其接口的所有引用时,将自动释放TXMLDocument实例。 When TXMLDocument is created with an Owner, however, it behaves like any other component, and is freed by its Owner. 但是,当使用所有者创建TXMLDocument时,它的行为与任何其他组件一样,并由其所有者释放。

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. 换句话说,在使用nil Owner创建TXMLDocument实例时, 不要在实例上调用Free()FreeAndNil() ,并且必须将对象分配给IXMLDocument变量,以便正确管理其现在活动的引用计数。

在运行时创建它时,您需要为TXMLDocument提供Owner

XMLDocument1 := TXMLDocument.Create(xml);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM