c++ char 内存泄漏与 valgrind 分析
c++ char memory leakage with valgrind analysis
我有大约 350 行代码,我正在解析 xml 文件并使用 C++ 从中创建另外 2 个工作表。
添加最后 2 或 3 个函数后,我开始出现内存错误,
Signal: SIGSEGV (Segmentation fault)
删除它们并没有解决错误,所以我肯定编辑了其他内容。我在网上搜索并找到了有关 valgrind 的信息,它向我显示了我无法翻译的错误。
根据 valgrind 手册,我必须从下到上检查,所以按照最后一个,
==7276== 4,064 bytes in 1 blocks are definitely lost in loss record 52 of 54
==7276== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7276== by 0x456BDD: ??? (in /usr/bin/g++-5)
==7276== by 0x4EC260D: _obstack_begin (obstack.c:176)
==7276== by 0x456FCE: ??? (in /usr/bin/g++-5)
==7276== by 0x43BA49: ??? (in /usr/bin/g++-5)
==7276== by 0x43BAC0: ??? (in /usr/bin/g++-5)
==7276== by 0x4E5A82F: (below 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 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(更多警告)
- -迂腐(关闭扩展并生成更多警告)
- -g(调试符号)
- -O3(最大优化,不推荐调试!)
我有大约 350 行代码,我正在解析 xml 文件并使用 C++ 从中创建另外 2 个工作表。
添加最后 2 或 3 个函数后,我开始出现内存错误,
Signal: SIGSEGV (Segmentation fault)
删除它们并没有解决错误,所以我肯定编辑了其他内容。我在网上搜索并找到了有关 valgrind 的信息,它向我显示了我无法翻译的错误。
根据 valgrind 手册,我必须从下到上检查,所以按照最后一个,
==7276== 4,064 bytes in 1 blocks are definitely lost in loss record 52 of 54
==7276== at 0x4C2DB8F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7276== by 0x456BDD: ??? (in /usr/bin/g++-5)
==7276== by 0x4EC260D: _obstack_begin (obstack.c:176)
==7276== by 0x456FCE: ??? (in /usr/bin/g++-5)
==7276== by 0x43BA49: ??? (in /usr/bin/g++-5)
==7276== by 0x43BAC0: ??? (in /usr/bin/g++-5)
==7276== by 0x4E5A82F: (below 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 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(更多警告)
- -迂腐(关闭扩展并生成更多警告)
- -g(调试符号)
- -O3(最大优化,不推荐调试!)