首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

关于TinyXML创建对象的释放解决思路

2012-02-02 
关于TinyXML创建对象的释放C/C++ code/* * create 2011-12-29 * 生成迁移导出文件 * InputParm: int* resu

关于TinyXML创建对象的释放

C/C++ code
/* * create 2011-12-29 * 生成迁移导出文件 * InputParm: int* result  执行结果 1[成功] 2[没有可以迁移的数据] 3[异常]  * InputParm: AnsiString* fileName 文件名 * InputParm: AnsiString* filePath 文件路径 * InputParm: TADOConnection* adoconnection 数据库连接 * InputParm: AnsiString* exmsg 异常消息 * OutputParm: void */void TBillDataManage::Generate_Migration_XML(                                             int* result,                                             AnsiString* fileName,                                             AnsiString* filePath,                                             TADOConnection* adoconnection,                                             AnsiString* exmsg                                             ){  AnsiString sqlscript;  sqlscript = "SELECT * FROM dt_ZXZ_Infos WHERE flag=1";  TADOQuery* query = new TADOQuery(NULL);  try  {    try    {      query->Connection = adoconnection;      query->SQL->Clear();      query->SQL->Add(sqlscript);      query->Open();      int rec;      rec = query->RecordCount;      int row;      row = 0;      if(rec > 0)      {        TiXmlDocument* tiXmlDocument = new TiXmlDocument();        TiXmlDeclaration *pDeclaration = new TiXmlDeclaration("1.0", "", "");  //文档定义        tiXmlDocument->LinkEndChild(pDeclaration);        TiXmlElement* xml_root = new TiXmlElement("DTT-DataExport");        tiXmlDocument->LinkEndChild(xml_root);        TiXmlElement* xml_Date = new TiXmlElement("Date");        TiXmlText* xml_Date_text = new TiXmlText(Now().DateTimeString().c_str());        xml_Date->LinkEndChild(xml_Date_text);        xml_root->LinkEndChild(xml_Date);        TiXmlElement* xml_Type = new TiXmlElement("Type");        TiXmlText* xml_Type_text = new TiXmlText(DataMigration.c_str());        xml_Type->LinkEndChild(xml_Type_text);        xml_root->LinkEndChild(xml_Type);        TiXmlElement* xml_DataArea = new TiXmlElement("DataArea");        TiXmlElement* xml_DataCount = new TiXmlElement("DataCount");        TiXmlText* xml_DataCount_text = new TiXmlText(IntToStr(rec).c_str());        xml_DataCount->LinkEndChild(xml_DataCount_text);        xml_DataArea->LinkEndChild(xml_DataCount);        TiXmlElement* xml_DataVector = new TiXmlElement("DataVector");        while(!query->Eof)        {          row += 1;          TiXmlElement* xml_Data = new TiXmlElement("Data");          xml_Data->SetAttribute("count", IntToStr(row).c_str());          TiXmlElement* xml_billnum = new TiXmlElement("billnum");          TiXmlText* xml_billnum_text = new TiXmlText(query->FieldByName("billnum")->AsString.c_str());          xml_billnum->LinkEndChild(xml_billnum_text);          TiXmlElement* xml_billtype = new TiXmlElement("billtype");          TiXmlText* xml_billtype_text = new TiXmlText(query->FieldByName("billtype")->AsString.c_str());          xml_billtype->LinkEndChild(xml_billtype_text);          TiXmlElement* xml_company = new TiXmlElement("company");          TiXmlText* xml_company_text = new TiXmlText(query->FieldByName("company")->AsString.c_str());          xml_company->LinkEndChild(xml_company_text);          TiXmlElement* xml_companycode = new TiXmlElement("companycode");          TiXmlText* xml_companycode_text = new TiXmlText(query->FieldByName("companycode")->AsString.c_str());          xml_companycode->LinkEndChild(xml_companycode_text);          TiXmlElement* xml_billyear = new TiXmlElement("billyear");          TiXmlText* xml_billyear_text = new TiXmlText(query->FieldByName("billyear")->AsString.c_str());          xml_billyear->LinkEndChild(xml_billyear_text);          TiXmlElement* xml_inserttime = new TiXmlElement("inserttime");          TiXmlText* xml_inserttime_text = new TiXmlText(query->FieldByName("inserttime")->AsString.c_str());          xml_inserttime->LinkEndChild(xml_inserttime_text);          TiXmlElement* xml_flag = new TiXmlElement("flag");          TiXmlText* xml_flag_text = new TiXmlText(query->FieldByName("flag")->AsString.c_str());          xml_flag->LinkEndChild(xml_flag_text);          TiXmlElement* xml_insertcompany = new TiXmlElement("insertcompany");          TiXmlText* xml_insertcompany_text = new TiXmlText(query->FieldByName("insertcompany")->AsString.c_str());          xml_insertcompany->LinkEndChild(xml_insertcompany_text);          xml_Data->LinkEndChild(xml_billnum);          xml_Data->LinkEndChild(xml_billtype);          xml_Data->LinkEndChild(xml_company);          xml_Data->LinkEndChild(xml_companycode);          xml_Data->LinkEndChild(xml_billyear);          xml_Data->LinkEndChild(xml_inserttime);          xml_Data->LinkEndChild(xml_flag);          xml_Data->LinkEndChild(xml_insertcompany);          xml_DataVector->LinkEndChild(xml_Data);          //DELETE                    //delete xml_insertcompany_text;          //delete xml_insertcompany;                    //delete xml_flag_text;          //delete xml_flag;          //delete xml_inserttime_text;          //delete xml_inserttime;          //delete xml_billyear_text;          //delete xml_billyear;          //delete xml_companycode_text;          //delete xml_companycode;          //delete xml_company_text;          //delete xml_company;          //delete xml_billtype_text;          //delete xml_billtype;          //delete xml_billnum_text;          //delete xml_billnum;                    //delete xml_Data;          query->Next();        }        xml_DataArea->LinkEndChild(xml_DataVector);        xml_root->LinkEndChild(xml_DataArea);        //生成文件名称        *fileName = "DataMigration_" + Now().DateString() + ".XML";        AnsiString m_filePath = *filePath + *fileName;        tiXmlDocument->SaveFile(m_filePath.c_str());        //delete xml_DataVector;        //delete xml_DataCount_text;        //delete xml_DataCount;        //delete xml_DataArea;        //delete xml_Type_text;        //delete xml_Type;        //delete xml_Date_text;        //delete xml_Date;        //delete xml_root;        //delete pDeclaration;        delete tiXmlDocument;        *result = RESULT_SUCESS;      }else      {        *result = RESULT_NODATA;      }    }catch(Exception &ex)    {      *result = RESULT_EXCEPTION;      *exmsg = ex.Message;    }  }__finally  {    query->Close();    query->Free();  }} 


各位好,呵呵,现在有一问题请教大家,最近使用TinyXML来生成和解析XML文件,生成和解析都没有问题,这个东西用着也很方便,在这里感谢下作者,呵呵!目前有一个问题,我上面已经贴出代码了,就是那些New出来的对象怎么释放,我一Delete就出错,比如我现delete xml_insertcompany_text; delete xml_insertcompany;不会出错,但是我接着释放下面的对象就报错了。delete tiXmlDocument;这一句如果我不释放我看了下生成几次内存占用就很高了,但是加上后连续生成上百文件看起来没有内存泄漏,但是不知道这样只 delete tiXmlDocument; 有没有问题,还请大家指教,以前写JAVA程序从没关注过内存这方面的问题,现在写C++,真是小心翼翼啊,在指针这东西上吃过大亏,呵呵,如果代码中还有其他问题请大家指出来,我改,谢谢大家了!
注:tinyxml_2_6_2.zip 我用的这个版本的TinyXML。
报错内容:Abnormal program termination 然后确定后就是 那个 内存不能为 "read"的提示了。


[解决办法]
TiXmlElement作为TiXmlDocument的子对象,TiXmlDocument可能负责了子对象的释放工作,所以,只需显式的delete TiXmlDocument对象就可以了。

热点排行