最近在开发iOS程序,调用WebService时候使用到了iOS内置的libxml2来作为DOM树的操作库,按照官方代码的参考,写了一段创建DOM的代码,部分片段如下:
xmlNodePtr generateSoap11MessageWrapper(void)
{
// create Document with Default version
const xmlDocPtr pDoc = xmlNewDoc(NULL);
{
// force version and encoding
pDoc->version = _XS"1.0";
pDoc->encoding = _XS"UTF-8";
}
// create Root-Element
const xmlNodePtr pRoot = xmlNewNode(NULL, _XS"Envelope");
{
// attach namespace definition to @pRoot->nsDef.
xmlNewNs(pRoot, _XS"http://www.w3.org/2001/XMLSchema-instance", _XS"xsi");
xmlNewNs(pRoot, _XS"http://www.w3.org/2001/XMLSchema", _XS"xsd");
const xmlNsPtr pNsSoap = xmlNewNs(pRoot, _XS"http://schemas.xmlsoap.org/soap/envelope/", _XS"soap");
// set the ns as the node namespace
xmlSetNs(pRoot, pNsSoap);
}
// set @pRoot as Document's root
xmlDocSetRootElement(pDoc, pRoot);
// assign Root-Element to a readable name (Envelope node)
const xmlNodePtr pEnvelope = pRoot;

在iOS应用开发中,使用libxml2解析XML时遇到程序Crash问题,特别是在调用xmlFreeDoc时。该问题在iOS4.2设备上尤为明显,表现为“pointer being free was not allocated”。通过搜索,发现在 GNOME 的 XML maillist 中有相关讨论,指出了解决此问题的具体原因和方法。解决方案在于理解xmlDoc内对象的释放规则,以避免内存管理错误。
最低0.47元/天 解锁文章
751

被折叠的 条评论
为什么被折叠?



