Delphi - 运行时创建的TXMLDocument会生成AV,表单上的组件正在运行 [英] Delphi - TXMLDocument created at run-time generates AV, with component on the form is working

查看:148
本文介绍了Delphi - 运行时创建的TXMLDocument会生成AV,表单上的组件正在运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行时创建一个TXMLDocument的实例,以加载和解析XML文件。您可以检查以下代码:

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.

问题是这是生成AV。您可能已经看到,在组件在表单(// XMLDocument1:TXMLDocument;)之前。

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

为什么组件在代码正在运行的表单上,但是当我在运行时创建它时会生成AV?

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:
解决方案:根据答案/评论和Delphi帮助:

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

XMLDocument1 : IXMLDocument;  //not TXMLDocument

XMLDocument1 := LoadXMLDocument(...);

FreeAndNil;// must be deleted


推荐答案

从我所知道的,您应该使用界面 IDoc:IXMLDocument;

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

从文档:


当TXMLDocument创建时没有所有者,它的行为就像一个
接口的对象。也就是说,当所有对其接口的引用都被释放为
时,TXMLDocument实例将被自动释放。当
TXMLDocument与所有者一起创建时,它的行为就像任何
其他组件一样,并由其所有者释放。

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.

换句话说,当使用 nil 所有者创建 TXMLDocument 实例时,不要在实例上调用 Free() FreeAndNil(),而必须将对象分配给一个 IXMLDocument 变量,使其现在有效的引用计数得到正确管理。

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.

这篇关于Delphi - 运行时创建的TXMLDocument会生成AV,表单上的组件正在运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆