使用valgrind分析的c ++ char内存泄漏

Mour_Ka

我有大约350行代码,我在其中解析xml文件并使用C ++从中创建另外2张纸。

在添加了最后2个或3个函数后,我开始出现内存错误,

信号:SIGSEGV(分段故障)

删除它们并不能解决错误,因此绝对可以编辑其他内容。我在网上搜索了有关valgrind的信息,该信息向我显示了我无法翻译的错误。

根据valgrind手册,我必须从下到上进行检查,以便按照最后一本进行检查,

== 7276 == 1个块中的4,064个字节肯定在丢失记录52 of 54中丢失了
== 7276 ==在0x4C2DB8F:malloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)==
7276 ==由0x456BDD:??? (在/ usr / bin / g ++-5中)
== 7276 ==通过0x4EC260D:_obstack_begin(obstack.c:176)
== 7276 ==通过0x456FCE:??? (在/ usr / bin / g ++-5中)
== 7276 ==通过0x43BA49:??? (在/ usr / bin / g ++-5中)
== 7276 ==通过0x43BAC0:??? (在/ usr / bin / g ++-5中)
== 7276 == by 0x4E5A82F:(main下方)(libc-start.c:291)

对于第291行是以下代码中的附加行,

if (vlan_ether)
sprintf(cConfStr, "**.Switch%ld.eth[%d].queue.VlanClassifier = true\n", srcindex + 1, portno);
else if (!vlan_ether)
sprintf(cConfStr, "**.Switch%ld.eth[%d].queue.etherType = false\n", srcindex + 1, portno);
else
cout << "Switch" << srcindex + 1 << "port# " << portno << " Classifier is not specified." << endl;
confStr.append(string(cConfStr));

删除上面的行并没有做任何更改,而第176行只是注释,因此让valgrind没有显示错误行是很犹豫的。

因此,我不确定要从代码中向您展示什么。我仍然确定它应该与字符串和char用法有关,在其中我多次使用了它们。

char cNedStr[600];
char cConfStr[600];

sprintf(cConfStr, "%d ", intervalvector[q]);
confStr.append(string(cConfStr)); 

Valgrind结果如下

amr@amr-PC:~$ valgrind --leak-check=yes  g++ main.cpp pugixml.cpp headers.h -Wall -std=c++11 

编辑:

amr@amr-PC:~/ClionProjects/converter$ valgrind ./a.out
==7624== Memcheck, a memory error detector
==7624== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==7624== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==7624== Command: ./a.out
==7624== Invalid free() / delete / delete[] / realloc()
==7624==    at 0x4C2F24B: operator delete(void*) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7624==    by 0x5714FF7: __run_exit_handlers (exit.c:82)
==7624==    by 0x5715044: exit (exit.c:104)
==7624==    by 0x56FB836: (below main) (libc-start.c:325)
==7624==  Address 0x35663a37323a6536 is not stack'd, malloc'd or (recently) free'd
==7624== 
==7624== 
==7624== HEAP SUMMARY:
==7624==     in use at exit: 72,704 bytes in 1 blocks
==7624==   total heap usage: 525 allocs, 525 frees, 214,887 bytes allocated
==7624== 
==7624== LEAK SUMMARY:
==7624==    definitely lost: 0 bytes in 0 blocks
==7624==    indirectly lost: 0 bytes in 0 blocks
==7624==      possibly lost: 0 bytes in 0 blocks
==7624==    still reachable: 72,704 bytes in 1 blocks
==7624==         suppressed: 0 bytes in 0 blocks
==7624== Rerun with --leak-check=full to see details of leaked memory
==7624== 
==7624== For counts of detected and suppressed errors, rerun with: -v
==7624== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
乔纳斯

第一次编译,不包含头文件(默认输出为a.out):

g++ main.cpp pugixml.cpp -Wall -std=c++11

然后使用valgrind检查:

valgrind --leak-check=yes ./a.out

其他的建议:

您可以考虑将以下标志添加到g ++:

  • -Wextra(更多警告)
  • -pedantic(关闭扩展名并生成更多警告)
  • -g(调试符号)
  • -O3(最大优化,不建议用于调试!)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章