我在 C/Linux 上使用 libxml2 从 xml 文件中提取信息。我创建了一个函数来查找某个标记的第一次出现并返回该标记的副本。
例如给定以下 xml 文本:
<a><b>First occurrence of tag b<c>Child node</c></b><b>Second occurrence of tag b</b></a>
我想提取第一个标签,如果存在,则包含所有子标签。
这是我使用的代码的简化版本:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libxml/parser.h>
#include <libxml/tree.h>
#ifdef LIBXML_TREE_ENABLED
static int
xml_extract_first_occurrence_by_name(xmlNode * start_node, xmlNode * first_occurrence, xmlChar * node_name)
{
xmlNode *cur_node = NULL;
for (cur_node = start_node; cur_node; cur_node = cur_node->next) {
if ( !xmlStrcmp(cur_node->name, node_name) ) {
// use libxml2 function xmlCopyNode to recursively copy cur_node (i.e. copy node + children) to first_occurrence
// (since cur_node will not be valid outside this function)
*first_occurrence = *xmlCopyNode(cur_node, 1);
return 1;
}
// if not found in parent search recursively in children
if (cur_node->children)
if ( xml_extract_first_occurrence_by_name(cur_node->children, first_occurrence, node_name) )
return 1;
}
}
int
main(int argc, char **argv)
{
xmlDoc *doc = NULL;
xmlNode *root_element = NULL;
if (argc != 2)
return(1);
// initialize libxml2 and check for version mismatches
LIBXML_TEST_VERSION
/*parse the file and get the DOM */
doc = xmlReadFile(argv[1], NULL, XML_PARSE_NOBLANKS);
/*Get the root element node */
root_element = xmlDocGetRootElement(doc);
// allocate memory for the node to be extracted (including up to 10 children)
xmlNode *first_occurrence = malloc(11 * sizeof(xmlNode));
// search in tag root_element the first occurrence of tag "b"
xml_extract_first_occurrence_by_name(root_element, first_occurrence, "b");
printf("\nFirst occurrence of tag b -> Text content: %s\n", first_occurrence->children->content);
printf("\nFirst occurrence of tag b -> Child tag: %s\n", first_occurrence->children->next->name);
printf("\nFirst occurrence of tag b -> Child tag text content: %s\n", first_occurrence->children->next->children->content);
free(first_occurrence);
/*free the document */
xmlFreeDoc(doc);
/*
*Free the global variables that may
*have been allocated by the parser.
*/
xmlCleanupParser();
return 0;
}
#else
int main(void) {
fprintf(stderr, "Tree support not compiled in\n");
exit(1);
}
#endif
程序按预期运行,也没有编译器警告。如果我通过 valgrind 运行它(启用泄漏检查),我会收到错误。
valgrind --leak-check=full ./first-occurrence-test.bin first-occurrence-test.xml
==18986== Memcheck, a memory error detector
==18986== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==18986== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==18986== Command: ./first-occurrence-test.bin first-occurrence-test.xml
==18986==
First occurrence of tag b -> Text content: First occurrence of tag b
First occurrence of tag b -> Child tag: c
First occurrence of tag b -> Child tag text content: Child node
==18986==
==18986== HEAP SUMMARY:
==18986== in use at exit: 281 bytes in 8 blocks
==18986== total heap usage: 77 allocs, 69 frees, 47,465 bytes allocated
==18986==
==18986== 281 (60 direct, 221 indirect) bytes in 1 blocks are definitely lost in loss record 7 of 7
==18986== at 0x402A17C: malloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==18986== by 0x409A01E: ??? (in /usr/lib/i386-linux-gnu/libxml2.so.2.9.1)
==18986== by 0x80487C0: xml_extract_first_occurrence_by_name (first-occurrence-test.c:30)
==18986== by 0x8048848: xml_extract_first_occurrence_by_name (first-occurrence-test.c:36)
==18986== by 0x80488FD: main (first-occurrence-test.c:63)
==18986==
==18986== LEAK SUMMARY:
==18986== definitely lost: 60 bytes in 1 blocks
==18986== indirectly lost: 221 bytes in 7 blocks
==18986== possibly lost: 0 bytes in 0 blocks
==18986== still reachable: 0 bytes in 0 blocks
==18986== suppressed: 0 bytes in 0 blocks
==18986==
==18986== For counts of detected and suppressed errors, rerun with: -v
==18986== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
我尝试删除该函数并在 main 中执行所有操作。相同的结果。
我在监督什么吗?这是 xmlCopyNode 可能存在的问题(或我不知道的问题)吗?还是误报?
感谢任何帮助或评论。
编辑:我让它工作,关键是确认 xmlCopyNode() 已经分配内存(无需再次分配)并使用 xmlFreeNode() 释放 xmlCopyNode() 分配的内存。谢谢你们的回答。
最佳答案
given that C passes by value, not by reference,
to change where a pointer points to,
a sub function needs the address of the actual pointer
therefore:
this line:
*first_occurrence = *xmlCopyNode(cur_node, 1);
has a few problems:
Below is what needs to be done to correct the problems
and eliminate the memory leak
-------------
note that:
the xmlCopyNode returns a pointer to a a copy of the original node.
I.E. it returns a pointer, that must be (eventually) passed to some form of free()
-------------
Note: the passed parameter: 'first_occurrence'
is already pointing to an allocated memory area via:
(this is where the leak starts)
xmlNode *first_occurrence = malloc(11 * sizeof(xmlNode));
-------------
suggest changing this line:
*first_occurrence = *xmlCopyNode(cur_node, 1);
to :
*first_occurrence = xmlCopyNode(cur_node, 1);
-------------
then remove the malloc so this line:
xmlNode *first_occurrence = malloc(11 * sizeof(xmlNode));
becomes:
xmlNode *first_occurrence = NULL;
-------------
finally change this line so the parameter contains the address of
rather than the contents of first_occurrence
xml_extract_first_occurrence_by_name(root_element, first_occurrence, "b");
to this:
xml_extract_first_occurrence_by_name(root_element, &first_occurrence, "b");
关于c - xmlCopyNode (libxml2) 内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27900525/
作为我的Rails应用程序的一部分,我编写了一个小导入程序,它从我们的LDAP系统中吸取数据并将其塞入一个用户表中。不幸的是,与LDAP相关的代码在遍历我们的32K用户时泄漏了大量内存,我一直无法弄清楚如何解决这个问题。这个问题似乎在某种程度上与LDAP库有关,因为当我删除对LDAP内容的调用时,内存使用情况会很好地稳定下来。此外,不断增加的对象是Net::BER::BerIdentifiedString和Net::BER::BerIdentifiedArray,它们都是LDAP库的一部分。当我运行导入时,内存使用量最终达到超过1GB的峰值。如果问题存在,我需要找到一些方法来更正我的代
ruby如何管理内存。例如:如果我们在执行过程中采用C程序,则以下是内存模型。类似于这个ruby如何处理内存。C:__________________|||stack|||------------------||||------------------|||||Heap|||||__________________|||data|__________________|text|__________________Ruby:? 最佳答案 Ruby中没有“内存”这样的东西。Class#allocate分配一个对象并返回该对象。这就是程序
你好,我无法成功如何在散列中删除key后释放内存。当我从哈希中删除键时,内存不会释放,也不会在手动调用GC.start后释放。当从Hash中删除键并且这些对象在某处泄漏时,这是预期的行为还是GC不释放内存?如何在Ruby中删除Hash中的键并在内存中取消分配它?例子:irb(main):001:0>`ps-orss=-p#{Process.pid}`.to_i=>4748irb(main):002:0>a={}=>{}irb(main):003:0>1000000.times{|i|a[i]="test#{i}"}=>1000000irb(main):004:0>`ps-orss=-p
这会导致Ruby出现内存问题吗?我知道如果大小超过10KB,Open-URI会写入TempFile。但是HTTParty会在写入TempFile之前尝试将整个PDF保存到内存吗?src=Tempfile.new("file.pdf")src.binmodesrc.writeHTTParty.get("large_file.pdf").parsed_response 最佳答案 您可以使用Net::HTTP。参见thedocumentation(特别是标题为“流媒体响应机构”的部分)。这是文档中的示例:uri=URI('http://e
我们在服务器端遇到libxml-rubygem的问题可能是因为它使用x86_64架构:$uname-aLinuxip-10-228-171-642.6.21.7-2.fc8xen-ec2-v1.0#1SMPTueSep110:25:30EDT2009x86_64GNU/Linuxrequire'libxml'LoadError:/usr/local/ruby-enterprise/lib/ruby/gems/1.8/gems/libxml-ruby-1.1.4/lib/libxml_ruby.so:invalidELFheader-/usr/local/ruby-enterprise/
在设置我的事件商家git时,我一直在为安装libxml-ruby这个错误而苦苦挣扎。我多次用谷歌搜索错误的所有组成部分,但我不太确定我在看什么。有一堆“不”——有人知道如何解释我可能遗漏了什么吗?Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallinglibxml-ruby:ERROR:Failedtobuildgemnativeextension./Users/tommynicholas/.rvm/rubies/ruby-2.1.1/bin/rubyextconf.rbcheckingforsocket(
在部署在heroku上的Rails应用程序(v:3.1)中,我在内存中获得了更多具有相同ID的对象。我的heroku控制台日志:>>Project.find_all_by_id(92).size=>2>>ActiveRecord::Base.connection.execute('select*fromprojectswhereid=92').to_a.size=>1这怎么可能?可能是什么问题? 最佳答案 解决方案根据您的SQL查询,您的数据库中显然没有重复条目。也许您的类项目中的size或length方法已被覆盖。我试过find_
我的两个不同的Rails应用程序的内存有一些奇怪的问题。这两个应用程序都使用rails3.0.7。每个Controller请求分配20-30-50MB的内存。在生产模式下,这个数量减少到5-10。但这是同样的事情。这是两个应用程序使用的gem列表:gem'pg'gem'haml'gem'sass'gem'devise'gem'simple_form'gem'state_machine'gem"globalize3","0.1.0.beta"gem"easy_globalize3_accessors"gem'paperclip'gem'andand'关闭所有这些gem不会给我任何结果。我
正如标题,我有一个处理大量数据的ruby程序。该程序占用了所有内存,其中调用了系统命令hostname,并且发生错误无法分配内存-主机名我试过GC.start但它不起作用。那么如何强制ruby释放未使用的内存呢?OK,这是别人的测试代码,最后报错是big_var被回收了。但是内存仍然没有释放。require"weakref"defreportputs"#{param}:\t\tMemory"+`psax-opid,rss|grep-E"^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)[1].to_s+'KB'endbig_var=""#big
我想上传我在运行时用Ruby生成的数据,就像从block中提供上传数据一样。我找到的所有示例仅展示了如何流式传输必须在请求之前位于磁盘上的文件,但我不想缓冲该文件。除了滚动我自己的套接字连接之外,最好的解决方案是什么?这是一个伪代码示例:post_stream('127.0.0.1','/stream/')do|body|generate_xmldo|segment|body 最佳答案 有效的代码。require'thread'require'net/http'require'base64'require'openssl'class