void CClientSocket::Encode(S_Data *sData,CString &xml)
{
TiXmlDocument doc;
TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "utf-8", "");
if(decl == NULL)
{
AfxMessageBox("CClientSocket::Encode error");
}
doc.LinkEndChild( decl ); TiXmlElement *lmtRoot = new TiXmlElement("command");
if(lmtRoot == NULL)
{
AfxMessageBox("CClientSocket::Encode error1");
}
doc.LinkEndChild(lmtRoot); TiXmlElement *lmtId = new TiXmlElement("id");
if(lmtId == NULL)
{
AfxMessageBox("CClientSocket::Encode error2");
}
lmtRoot->LinkEndChild(lmtId);
CString tmp;
tmp.Format("%d",sData->commandId);
TiXmlText *txtId = new TiXmlText(tmp);
if(txtId == NULL)
{
AfxMessageBox("CClientSocket::Encode error3");
}
lmtId->LinkEndChild(txtId); TiXmlElement *lmtName = new TiXmlElement("name");
if(lmtName == NULL)
{
AfxMessageBox("CClientSocket::Encode error4");
}
lmtRoot->LinkEndChild(lmtName);
TiXmlText *txtName = new TiXmlText(sData->commandName);
if(txtName == NULL)
{
AfxMessageBox("CClientSocket::Encode error5");
}
lmtName->LinkEndChild(txtName); TiXmlElement *lmtType = new TiXmlElement("type");
if(lmtType == NULL)
{
AfxMessageBox("CClientSocket::Encode error6");
}
lmtRoot->LinkEndChild(lmtType);
TiXmlText *txtType = new TiXmlText(sData->type);
if(txtType == NULL)
{
AfxMessageBox("CClientSocket::Encode error7");
}
lmtType->LinkEndChild(txtType); TiXmlElement *lmtParamRoot = new TiXmlElement("params");
if(lmtParamRoot == NULL)
{
AfxMessageBox("CClientSocket::Encode error8");
}
lmtRoot->LinkEndChild(lmtParamRoot); for (int i = 0; i < sData->params.size(); i ++)
{
TiXmlElement *lmtTmp = new TiXmlElement("param");
if(lmtTmp == NULL)
{
AfxMessageBox("CClientSocket::Encode error9");
}
lmtParamRoot->LinkEndChild(lmtTmp); TiXmlElement *lmtKey = new TiXmlElement("key");
if(lmtKey == NULL)
{
AfxMessageBox("CClientSocket::Encode error10");
}
lmtTmp->LinkEndChild(lmtKey);
TiXmlText *txtKey = new TiXmlText((CString)sData->params.at(i));
if(txtKey == NULL)
{
AfxMessageBox("CClientSocket::Encode error11");
}
lmtKey->LinkEndChild(txtKey); TiXmlElement *lmtValue = new TiXmlElement("value");
if(lmtValue == NULL)
{
AfxMessageBox("CClientSocket::Encode error12");
}
lmtTmp->LinkEndChild(lmtValue);
TiXmlText *txtValue = new TiXmlText((CString)sData->params.at(i + 1));
if(txtValue == NULL)
{
AfxMessageBox("CClientSocket::Encode error13");
}
lmtValue->LinkEndChild(txtValue);
i ++;
} //doc.SaveFile( "d:\\test.XML" );
TiXmlPrinter printer;
doc.Accept(&printer);
xml.Format("%s",printer.CStr());
/*是否需要DELETE?
//20130222 add by cq
delete lmtParamRoot;
delete txtType;
delete lmtType;
delete txtName;
delete lmtName;
delete txtId;
delete lmtId;
delete lmtRoot;
delete decl;
*/}这是我的xml编码函数。原来所有的NEW的指针都没有DELETE,但我加了DELETE又会出错所以我现在的问题是。这些new难到不需要DELETE,如果需要又应该如何DELETE.
或者谁给个正确的XML编码的类。感谢!VC XML编码的内存释放
{
TiXmlDocument doc;
TiXmlDeclaration *decl = new TiXmlDeclaration("1.0", "utf-8", "");
if(decl == NULL)
{
AfxMessageBox("CClientSocket::Encode error");
}
doc.LinkEndChild( decl ); TiXmlElement *lmtRoot = new TiXmlElement("command");
if(lmtRoot == NULL)
{
AfxMessageBox("CClientSocket::Encode error1");
}
doc.LinkEndChild(lmtRoot); TiXmlElement *lmtId = new TiXmlElement("id");
if(lmtId == NULL)
{
AfxMessageBox("CClientSocket::Encode error2");
}
lmtRoot->LinkEndChild(lmtId);
CString tmp;
tmp.Format("%d",sData->commandId);
TiXmlText *txtId = new TiXmlText(tmp);
if(txtId == NULL)
{
AfxMessageBox("CClientSocket::Encode error3");
}
lmtId->LinkEndChild(txtId); TiXmlElement *lmtName = new TiXmlElement("name");
if(lmtName == NULL)
{
AfxMessageBox("CClientSocket::Encode error4");
}
lmtRoot->LinkEndChild(lmtName);
TiXmlText *txtName = new TiXmlText(sData->commandName);
if(txtName == NULL)
{
AfxMessageBox("CClientSocket::Encode error5");
}
lmtName->LinkEndChild(txtName); TiXmlElement *lmtType = new TiXmlElement("type");
if(lmtType == NULL)
{
AfxMessageBox("CClientSocket::Encode error6");
}
lmtRoot->LinkEndChild(lmtType);
TiXmlText *txtType = new TiXmlText(sData->type);
if(txtType == NULL)
{
AfxMessageBox("CClientSocket::Encode error7");
}
lmtType->LinkEndChild(txtType); TiXmlElement *lmtParamRoot = new TiXmlElement("params");
if(lmtParamRoot == NULL)
{
AfxMessageBox("CClientSocket::Encode error8");
}
lmtRoot->LinkEndChild(lmtParamRoot); for (int i = 0; i < sData->params.size(); i ++)
{
TiXmlElement *lmtTmp = new TiXmlElement("param");
if(lmtTmp == NULL)
{
AfxMessageBox("CClientSocket::Encode error9");
}
lmtParamRoot->LinkEndChild(lmtTmp); TiXmlElement *lmtKey = new TiXmlElement("key");
if(lmtKey == NULL)
{
AfxMessageBox("CClientSocket::Encode error10");
}
lmtTmp->LinkEndChild(lmtKey);
TiXmlText *txtKey = new TiXmlText((CString)sData->params.at(i));
if(txtKey == NULL)
{
AfxMessageBox("CClientSocket::Encode error11");
}
lmtKey->LinkEndChild(txtKey); TiXmlElement *lmtValue = new TiXmlElement("value");
if(lmtValue == NULL)
{
AfxMessageBox("CClientSocket::Encode error12");
}
lmtTmp->LinkEndChild(lmtValue);
TiXmlText *txtValue = new TiXmlText((CString)sData->params.at(i + 1));
if(txtValue == NULL)
{
AfxMessageBox("CClientSocket::Encode error13");
}
lmtValue->LinkEndChild(txtValue);
i ++;
} //doc.SaveFile( "d:\\test.XML" );
TiXmlPrinter printer;
doc.Accept(&printer);
xml.Format("%s",printer.CStr());
/*是否需要DELETE?
//20130222 add by cq
delete lmtParamRoot;
delete txtType;
delete lmtType;
delete txtName;
delete lmtName;
delete txtId;
delete lmtId;
delete lmtRoot;
delete decl;
*/}这是我的xml编码函数。原来所有的NEW的指针都没有DELETE,但我加了DELETE又会出错所以我现在的问题是。这些new难到不需要DELETE,如果需要又应该如何DELETE.
或者谁给个正确的XML编码的类。感谢!VC XML编码的内存释放
解决方案 »
- win7下添加ocx时说“不支持此接口”
- 端口重用问题,100分,帮帮忙
- 在非visual studio下写gui hello world,怎么加图标
- 求助:用MFC编写一对话框实现一些将输入转化为HTML的小功能
- ┐┐┐┐┐┐┐┐┐┐┐┐┐┐关于“API HooK”的菜鸟问题。
- ICMP中的校验和是什么意思,有什么作用?
- 在线求救关于Tab Control控件实现
- 如何取得查询结果呢?
- 高手快帮忙!急!
- error C2146: syntax error : missing ; before identifier lpddsprimary
- 背景加载一张255,0,255颜色的矩形//为什么不行???
- 1 条网线2台电脑如何用c/s模式
所以我想看看有没有熟悉XML解码的看看上面的做法是不是正确的。
不用delete。其实你稍微百度一下就可以查到,doc在析构时会delete所有的节点。