简体   繁体   English

Valgrind 检测到内存泄漏,找不到 C

[英]Valgrind detects memory leak, can't find it C

Valgrind is finding memory leaks but i can't seem to pinpoint them, i'm hope-full someone here can help me: Valgrind 正在发现内存泄漏,但我似乎无法确定它们,我希望这里有人可以帮助我:

在此处输入图片说明

在此处输入图片说明

main calls are Dictionary* dictionary = initDictionary();主要调用是Dictionary* dictionary = initDictionary();

Your initDictionary doesn't return the pointer dictionary anywhere.您的initDictionary不会在任何地方返回指针dictionary

That means when you do这意味着当你做

Dictionary* dictionary = initDictionary();

the value of dictionary will be indeterminate (seemingly random or garbage), and dereferencing this pointer or passing it to free will result in undefined behavior . dictionary的值将是不确定的(看似随机或垃圾),并且取消引用此指针或将其传递给free将导致未定义的行为

You solve this by adding a simple你通过添加一个简单的来解决这个问题

return dictionary;

at the end of the initDictionary function.initDictionary函数的末尾。


If your compiler doesn't warn you about not returning anything from the function, you need to enable more verbose warnings.如果您的编译器没有警告您不要从函数返回任何内容,您需要启用更详细的警告。 Using gcc or clang I recommend the options -Wall -Wextra -Wpedantic when building.使用gccclang我建议在-Wall -Wextra -Wpedantic时使用选项-Wall -Wextra -Wpedantic For MSVC use /W4 .对于 MSVC 使用/W4

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM