关于xmlCreateFileParserCtxt返回值的内存释放疑问
关于xmlCreateFileParserCtxt返回值内存分配的疑惑
我对xmlCreateFileParserCtxt的返回值存在疑惑:我认为它返回的指针是新分配的内存,必须用xmlFreeParserCtxt释放。但移除xmlFreeParserCtxt语句后,程序既不报错,且通过-fsanitize=address检测也没发现内存泄漏(确认返回值不为NULL)。想请教:如果xmlCreateFileParserCtxt返回非NULL值,有没有可能它并非指向新分配的内存?
我的测试代码如下:
#include <stdio.h> #include <libxml/xmlreader.h> #include <libxml/xmlwriter.h> #include <libxml/parser.h> #include <libxml/tree.h> #include <libxml/xpath.h> #include <libxml/xpathInternals.h> #include <libxml/parserInternals.h> #include <libxml/HTMLparser.h> #include <libxml/HTMLtree.h> #include <libxml/xinclude.h> #include <libxml/catalog.h> #include <libxml/uri.h> #include <libxml/valid.h> #include <libxml/xmlsave.h> #include <libxml/nanoftp.h> #include <libxml/schemasInternals.h> #include <libxml/relaxng.h> #include <libxml/xmlschemas.h> #include <libxml/dict.h> #include <libxml/pattern.h> #include <libxml/hash.h> #include <libxml/xmlversion.h> int main() { const char* xmlFilePath = "example.xml"; // Create a parser context for parsing an XML file xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt(xmlFilePath); if (ctxt == NULL) { printf("Failed to create parser context for file: %s\n", xmlFilePath); return 1; } // Cleanup // xmlFreeParserCtxt(ctxt); return 0; }
测试过程:
- 先添加
xmlFreeParserCtxt(ctxt);语句,确认程序运行无报错; - 之后删除该语句,用
gcc ./test.c -g -lxml2 -fsanitize=address编译代码并运行,本以为会输出内存泄漏的错误信息,但实际未发现任何相关输出。
内容的提问来源于stack exchange,提问作者icy17
相关产品推荐
相关产品推荐

