Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在xmlTextReaderGetAttribute()之后释放xmlChar指针_C++_C_Free_Libxml2 - Fatal编程技术网

C++ 在xmlTextReaderGetAttribute()之后释放xmlChar指针

C++ 在xmlTextReaderGetAttribute()之后释放xmlChar指针,c++,c,free,libxml2,C++,C,Free,Libxml2,我以前成功地使用了xmlTextReaderGetAttribute(from),但是API文档要求我释放返回的xmlChar*。现在,我的应用程序在第二次调用free()时崩溃(第一次调用为null),如下所示: xmlTextReaderPtr reader = null; xmlChar *attribVal = null; //blah... if (xmlTextReaderAttributeCount(reader) > 0) { free((attribVal));

我以前成功地使用了
xmlTextReaderGetAttribute
(from),但是API文档要求我释放返回的
xmlChar*
。现在,我的应用程序在第二次调用
free()
时崩溃(第一次调用为null),如下所示:

xmlTextReaderPtr reader = null;
xmlChar *attribVal = null;
//blah...
if (xmlTextReaderAttributeCount(reader) > 0) {
    free((attribVal));

attribVal = xmlTextReaderGetAttribute(reader, (const xmlChar*)"super-Attrib");
if (xmlStrcasecmp(attribVal, (const xmlChar*)"monoMega-Attrib") == 0) {
    free((attribVal));

我的项目是C++,但是LIXXML2和所有使用标准C.< /P> < P>的例子都使用<代码> XMLFIELD()/CUD>而不是<代码> For()/<代码>:

xmlTextReaderPtr reader = null; 
xmlChar *attribVal = null; 
//blah... 
if (xmlTextReaderAttributeCount(reader) > 0)
{ 
    attribVal = xmlTextReaderGetAttribute(reader, BAD_CAST "super-Attrib"); 
    if (attribVal)
    {
        ...
        xmlFree(attribVal);
    }
} 

这不是你的问题,但是为什么你要用两套参数写
免费(())
??什么是你的项目崩溃了(segfault?),你能在那一秒之前和之后直接打印出来,以确保它在那里崩溃吗?@Dan,我正在用gdb处理它,它在那里崩溃了。doube parens(())将强制执行
const
ness或void*。我把它们放在一个函数中,直到我发现它崩溃。有没有办法延迟加载
xmlFree
,尽管在
globals.h
中定义和重新定义了它?根据编译libxml时使用的定义,
xmlFree
是调用
\uxmlfree()的宏
然后取消引用它返回的函数指针,或者默认情况下它是指向
xmlMemFree()
free()
的函数指针变量。因此,您必须考虑到
xmlFree
实际上是什么,在后一种情况下,您可以延迟加载它实际指向的任何函数(或者只指向您自己的内存函数)。非常感谢!在这个问题上我得到了最全面的答案。我到家后会试试的。