You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

关于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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 00:27:42