我有大约350行代码,我正在解析xml文件并使用C ++创建另外两张代码。
添加最后2或3个函数后,我开始出现内存错误,
信号:SIGSEGV(分段故障)
删除它们并没有解决错误所以我肯定编辑了其他内容。我在网上搜索了一下valgrind,发现了我无法翻译的错误。
根据valgrind手册,我必须从下到上检查,以便按照最后一个,
== 7276 == 4个块中的4,064个字节肯定会丢失在54的丢失记录52中 == 7276 ==在0x4C2DB8F:malloc(在/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
== 7276 == by 0x456BDD:??? (在/ usr / bin / g ++ - 5中)
== 7276 == by 0x4EC260D:_obstack_begin(obstack.c:176)
== 7276 == by 0x456FCE:??? (在/ usr / bin / g ++ - 5中)
== 7276 == by 0x43BA49:??? (在/ usr / bin / g ++ - 5中)
== 7276 == by 0x43BAC0:??? (在/ usr / bin / g ++ - 5中)
== 7276 == by 0x4E5A82F :(主要)(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)
答案 0 :(得分:1)
首先编译,没有头文件,(默认输出为a.out
):
g++ main.cpp pugixml.cpp -Wall -std=c++11
然后使用valgrind检查:
valgrind --leak-check=yes ./a.out
其他评论:
您可以考虑将以下标志添加到g ++: