创建TXMLDocument对象时,用nil做参数,使用对象时弹出内存错误, 用self或Application就正常, 即:XMLDoc :=  TXMLDocument.Create(nil);
XMLDoc.LoadFromFile('D:\test.xml');
xn := XMLDoc.Node;
xn := xn.ChildNodes.Last;
Max := xn.ChildNodes.Count;  <----此处报内存错误!!但是如果
XMLDoc :=  TXMLDocument.Create(self);

XMLDoc :=  TXMLDocument.Create(Apllication);
就正常请教 如何解释这种情况???不胜感激!!

解决方案 »

  1.   

    已经在以前贴子三次回答这问题了。。

    var
      XMLDoc:TXMLDocument;
    改成:
      XMLDoc:IXMLDocument;-------------------------------
    当 TXMLDocument.Create的时候,
    其中参数为NIL时候,必须使用接口引用方法,
    否则参数必须为它提供了一个OWNER。它的内存不要去使用FREE释放,它是自动引用计数维护的。
      

  2.   

    奥秘主要这几句源码上面。。function TXMLDocument._AddRef: Integer;
    begin
      Result := InterlockedIncrement(FRefCount)
    end;function TXMLDocument._Release: Integer;
    begin
      Result := InterlockedDecrement(FRefCount);
      { If we are not being used as a TComponent, then use refcount to manage our
        lifetime as with TInterfacedObject. }
      if (Result = 0) and not FOwnerIsComponent then
        Destroy;
    end;