TXMLDocument销毁时调用Free出错?
本帖最后由 tjs_125 于 2013-08-11 23:09:15 编辑 我在程序中需要动态创建TXMLDocument对象,在用完后需要释放对象,但是释放时老是出错,代码如下
var
Axml: TXMLDocument;
begin
Axml := TXMLDocument.Create(nil);
// do something
FreeAndNil(Axml);
end;
上面代码中销毁对象一直出错,我知道在创建这个TXMLDocument对象时应该给定一个AOwner,但是创建的所属对象不是从TComponent继承的,无法用self来代替,我只好用了nil,而且最后我只好是
Axml := nil;来代替销毁。
请问大家,这样该怎么办啊?
[解决办法]
按下面的修改试下:
function TSDLC_800D.CreateCommonMsgXml(AMsgType: T800DMsgType; ABuildingId,
AGatewayId: string): string;
var
Axml: TXMLDocument;
root, xn, xnChild: IXMLNode;
strMsgType: string;
begin
Axml := TXMLDocument.Create(self);//修改self
Axml.XML.Clear;//修改
Axml.Active := True;
Axml.Version := '1.0';
Axml.Encoding := 'UTF-8';
Axml.Options := [doNodeAutoCreate, doNodeAutoIndent, doAttrNull, doAutoPrefix, doNamespaceDecl];
root := Axml.AddChild('root');
xn := root.AddChild('common');
xnChild := xn.AddChild('building_id');
xnChild.Text := ABuildingId;
xnChild := xn.AddChild('gateway_id');
xnChild.Text := AGatewayId;
xnChild := xn.AddChild('type');
case AMsgType of
mtarchives_quest: strMsgType := 'archives_quest';
mtarchives: strMsgType := 'archives';
mtreply_quest: strMsgType := 'reply_quest';
mtreply: strMsgType := 'reply';
mtcmd_quest: strMsgType := 'cmd_quest';
mtcmd_ack: strMsgType := 'cmd_ack';
mtconfig: strMsgType := 'config';
else ;
strMsgType := '';
end;
xnChild.Text := strMsgType;
result := Axml.XML.Text;
Axml.Free;//Axml := nil; //修改
end;
[解决办法]
看TXMLDocument的声明
TXMLDocument = class(TComponent, IInterface, IXMLDocument, IXMLDocumentAccess);