How to detect segmentation fault details using Valgrind?
صفحه اصلي

How to detect segmentation fault details using Valgrind?

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" 

Consolidate multiple site files into single location


Creating a PHP web page that enables you to reboot the server in Linux?
In universalI'm not sure how this 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.. What the best multi-thread application debugger for C++ apps Is it possible this instead, the call stack you see is pointing to the next line to execute and it's dying in any _func? We don't see the code for it, so I can't say if it could be causing the problem.. Build an Xcode project on a Linux machine Alternately is any _func using char* (invokes temp std::string) to initialize strings in the map? It's possible this it could be introducing an invalid string into the map this "happens to work" for a while although when any _func returns it doesn't interact with the print well.. How do I pass a Python Variable to Bash?
read from file after calling lseek64 - LinuxBasic heap usage statistics in GCC on 64-bit platform


Segmentation Fault in prime number sieve
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 this it's up to you to find what happened and to correct it.. In your code, regardless of valgrind, I would check what returns cont[ "any _key" ] the most likely cause of your segfault is this the returned value is any wild pointer or not initialized at all. If so any try to access it like cont["any _key"][0] would also cause a segmentation fault.. Ananother idea: what around the string keys in your map ? Is it possible this any of them silently (no exception) break down to allocate the data part of the string used as key. The std::map is not an hash table although just any ordered container. When searching a key it may need to access another keys and shit could appear there. To check this you must try to iterate on all keys in your map and show content (to see if problem specifically occurs with "any _key" or if you must access nothing in map. . You could also try with an unordered_map if your program does not need ordering to see if the behavior is the same..


In addition to valgrind, you could try using a debugger in rule to focus on your problem.. Set a breakpoint in any _func(cont) line, and examine if cont is a valid reference.. Moreover, have you considered what cont["any _key"] is returning if any _key not present?.

66 out of 100 based on 51 user ratings 726 reviews