如何使用Valgrind检测细分故障详细信息? [英] How to detect segmentation fault details using Valgrind?
问题描述
我有一个std :: map<std :: string,std :: string>,它通过一些API调用进行了初始化.当我尝试使用此地图时,出现分段错误.如何检测无效的代码或无效的内容或可以帮助我解决问题的任何详细信息?代码如下:
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:
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"
推荐答案
一般而言,我不确定该行如何产生段错误:方括号运算符将始终返回std :: string(创建一个空的(如果需要的话),并且对打印始终有效.
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.
是否有可能,您看到的调用堆栈指向了要执行的下一行,并且死在some_func中?我们看不到它的代码,所以我不能说这是否可能导致问题.
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
是否使用char *(调用temp std :: string)来初始化映射中的字符串?可能是在地图中引入了一个无效的字符串,该字符串会开始工作"一段时间,但是当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.
这篇关于如何使用Valgrind检测细分故障详细信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!