bison/flex 解析器的 Valgrind memcheck 结果分析
我正在我的程序上运行 memcheck,并试图解决内存问题。
Memcheck 输出:
==29633== 3,443 (128 direct, 3,315 indirect) bytes in 2 blocks are definitely lost in loss record 7 of 8
==29633== at 0x4A07D2E: operator new(unsigned long) (vg_replace_malloc.c:261)
==29633== by 0x42027F: PDcbk(cpfrCallbackType_e, Powerdomain*) (NetExtractor.cpp:1243)
==29633== by 0x413DBA: cpfparse() (cpf.y:120)
==29633== by 0x42039B: loadCPF(char*) (NetExtractor.cpp:1253)
==29633== by 0x420E5D: main (NetExtractor.cpp:1399)
==29633== LEAK SUMMARY:
==29633== definitely lost: 128 bytes in 2 blocks
==29633== indirectly lost: 3,315 bytes in 10 blocks
==29633== possibly lost: 0 bytes in 0 blocks
==29633== still reachable: 16,458 bytes in 3 blocks
==29633== suppressed: 0 bytes in 0 blocks
NetExtractor 的第 1253 行是:
powerdomainMap.insert(Powerdomain_pair(powerdomain->getName(),new Powerdomain(*powerdomain)));
用于理解代码的信息:
PowerdomainHashMap powerdomainMap;
typedef hash_map<const string, Powerdomain*, strptrhash, strptrequal> PowerdomainHashMap;
typedef pair<const string, Powerdomain*> Powerdomain_pair;
目前我的猜测是,实际上并没有泄漏,因为我仍然可以从哈希映射访问新创建的 powerdomain,而 valgrind 无法看到这一点。
我说得对吗?如果没有有人可以解释我吗?
谢谢,请随意询问代码详细信息,因为我只粘贴了我认为相关的内容,但我可能错过了一些东西。
I am running memcheck on my program and i'm trying to solve the memory issues.
Memcheck output :
==29633== 3,443 (128 direct, 3,315 indirect) bytes in 2 blocks are definitely lost in loss record 7 of 8
==29633== at 0x4A07D2E: operator new(unsigned long) (vg_replace_malloc.c:261)
==29633== by 0x42027F: PDcbk(cpfrCallbackType_e, Powerdomain*) (NetExtractor.cpp:1243)
==29633== by 0x413DBA: cpfparse() (cpf.y:120)
==29633== by 0x42039B: loadCPF(char*) (NetExtractor.cpp:1253)
==29633== by 0x420E5D: main (NetExtractor.cpp:1399)
==29633== LEAK SUMMARY:
==29633== definitely lost: 128 bytes in 2 blocks
==29633== indirectly lost: 3,315 bytes in 10 blocks
==29633== possibly lost: 0 bytes in 0 blocks
==29633== still reachable: 16,458 bytes in 3 blocks
==29633== suppressed: 0 bytes in 0 blocks
Line 1253 of NetExtractor is :
powerdomainMap.insert(Powerdomain_pair(powerdomain->getName(),new Powerdomain(*powerdomain)));
Info to understand the code :
PowerdomainHashMap powerdomainMap;
typedef hash_map<const string, Powerdomain*, strptrhash, strptrequal> PowerdomainHashMap;
typedef pair<const string, Powerdomain*> Powerdomain_pair;
For now my guess is that there isn't really a leak because i still can access the newly created powerdomain from my hashmap and that valgrind isn't able to see that.
Am I right ? If not can someone explain me ?
Thanks and feel free to ask code details as i only pasted what i thought was relevant but i might have missed something.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,您已经有了指向 Powerdomain 类动态分配对象的指针映射。
哈希映射本身没有分配该内存,因此它不会为您清理它。
您所要做的就是在不需要时自行释放该内存,例如:
您可能从未这样做过,而 Valgrind 告诉您在程序终止时该内存仍然可以访问。
现在,如果您在应用程序的生命周期内确实需要这些数据,那么就可以了,因为无论如何,操作系统都会在程序退出时清理该内存。但当然,最好是彻底关闭,这样当这个问题成为真正的问题时,您就不会因为误认为正常行为而忽视它。
除此之外,看起来你无论如何都在进行对象复制,所以我认为考虑不显式使用动态内存并让 hash_map 担心它可能是值得的。数据类型声明将如下所示:
希望有帮助。
Well, you have map of pointers to dynamically allocated objects of class
Powerdomain
.The hash map itself did not allocate that memory, so it doesn't clean it up for you.
What you have to do is to free that memory yourself once you don't need it, for example:
You probably never did that and Valgrind told you about that memory still being reachable upon the program termination.
Now, if you really need that data during the life time of your application, then it is OK because OS will clean that memory when program exits anyway. But of course it is much better to do a clean shutdown, so that when this problems becomes a real problem you don't ignore it by mistreating as OK behaviour.
Aside from that, it seems like you are doing object copies anyway so I think it might be worth it to consider not working with dynamic memory explicitly and let hash_map worry about it. The data type declaration will look like this:
Hope it helps.
根据 memcheck 的内存泄漏是指在程序生命周期内分配但程序退出时未释放的任何内存块。由于您在
hash_map
中存储了一个指针,因此当hash_map
被销毁时,该指针将保持未释放状态。您应该在 hash_map 中使用shared_ptr
或使用接受指针并在集合被销毁时删除它的集合。A memory leak according to memcheck is any block of memory which was allocated during the program lifetime which is not freed by program exit. Because you're storing a pointer in the
hash_map
, this pointer is left unfreed when thehash_map
is destroyed. You should either use ashared_ptr
in the hash_map or use a collection which accepts a pointer and deletes it when the collection is destroyed.在每个 valgrind 附带的手册的常见问题解答部分中,有:
可能你的问题是相关的。
In the FAQ section of the manual that comes with every valgrind, there is:
Possibly your problem is related.
您在插入操作中分配内存,该内存是否已正确释放?
我应该建议使用 STL 容器,它可以最大限度地减少您必须编写的代码量:
这应该具有您需要的所有功能以及内置内存安全性。
在 C++98 中,使用
和std::tr1::unordered_map
等。You are allocating memory in your insert operation, does that ever get deallocated properly?
I should recommend using STL containers which minimize the amount of code you have to write:
This should have all the functionality you need with built-in memory safety.
In C++98, use
<tr1/unordered_map>
andstd::tr1::unordered_map
etc.