C++ 如何使用 Valgrind 检测分段错误细节?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2683778/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to detect segmentation fault details using Valgrind?
提问by Davit Siradeghyan
I have a std::map< std::string, std::string> which initialized with some API call. When I'm trying to use this map I'm getting segmentation fault. How can I detect invalid code or what is invalid or any detail which can help me to fix problem? Code looks like this:
我有一个用一些 API 调用初始化的 std::map< std::string, std::string> 。当我尝试使用此地图时,出现分段错误。如何检测无效代码或无效代码或任何可以帮助我解决问题的细节?代码如下所示:
std::map< std::string, std::string> cont;
some_func( cont ); // getting parameter by reference and initialize it, someone corrupted memory (cont) inside this function
std::cout << cont[ "some_key" ] << '\n'; // segmentation fault here, cannot access "some_key"
采纳答案by Mark B
In general I'm not sure how that line could be generating a seg fault: the bracket operator will always return a std::string (creating an empty one if needed) and it should always be valid for printing.
一般来说,我不确定该行如何生成段错误:括号运算符将始终返回 std::string (如果需要,创建一个空字符串)并且它应该始终对打印有效。
Is it possible that instead, the call stack you see is pointing to the next line to execute and it's dying in some_func? We don't see the code for it, so I can't say if it could be causing the problem.
相反,您看到的调用堆栈是否可能指向要执行的下一行并且在 some_func 中消亡?我们没有看到它的代码,所以我不能说它是否会导致问题。
Alternately is some_func
using char* (invokes temp std::string) to initialize strings in the map? It's possible that it could be introducing an invalid string into the map that "happens to work" for a while but when some_func returns it doesn't interact with the print well.
或者some_func
使用 char*(调用 temp std::string)来初始化映射中的字符串?它可能会在地图中引入一个无效的字符串,该字符串“碰巧工作”了一段时间,但是当 some_func 返回时,它不会与打印很好地交互。
回答by kriss
you launch your application (compiled in debug mode) with the syntax:
您使用以下语法启动您的应用程序(在调试模式下编译):
valgrind yourapp
Valgrind will show you the stack backtrace of where segmentation fault occured. After that it's up to you to find what happened and to correct it.
Valgrind 将向您展示发生分段错误的堆栈回溯。之后,由您来找出发生的情况并进行纠正。
In your code, regardless of valgrind, I would check what returns cont[ "some_key" ]
the most likely cause of your segfault is that the returned value is some wild pointer or not initialized at all. If so any try to access it like cont["some_key"][0]
would also cause a segmentation fault.
在您的代码中,无论 valgrind 是什么,我都会检查返回段错误 cont[ "some_key" ]
的最可能原因是返回的值是某个野指针或根本未初始化。如果是这样,任何尝试访问它cont["some_key"][0]
也会导致分段错误。
Another idea: what about the string keys in your map ? Is it possible that some of them silently (no exception) failed to allocate the data part of the string used as key. The std::map is not an hash table but just some ordered container. When searching a key it may need to access other keys and shit could happen there. To check that you can try to iterate on all keys in your map and show content (to see if problem specifically occurs with "some_key" or if you can access nothing in map.
另一个想法:地图中的字符串键怎么样?是否有可能其中一些默默地(无一例外)未能分配用作键的字符串的数据部分。std::map 不是哈希表,而只是一些有序的容器。在搜索密钥时,它可能需要访问其他密钥,而在那里可能会发生一些糟糕的事情。要检查您是否可以尝试迭代地图中的所有键并显示内容(以查看“some_key”是否特别出现问题,或者您是否无法访问地图中的任何内容。
You could also try with an unordered_map if your program does not need ordering to see if the behavior is the same.
如果您的程序不需要排序来查看行为是否相同,您也可以尝试使用 unordered_map。
回答by Tom
In addition to valgrind, you could try using a debugger in order to focus on your problem.
除了 valgrind,您还可以尝试使用调试器来专注于您的问题。
Set a breakpoint in some_func(cont)
line, and examine if cont
is a valid reference.
some_func(cont)
在行中设置断点,并检查是否cont
是有效引用。
Moreover, have you considered what cont["some_key"]
is returning if some_key not present?
此外,您是否考虑过cont["some_key"]
如果 some_key 不存在会返回什么?