C++ segmentation fault in std::string function [closed] - c++

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm developing an algorithm based on Skyline queries using C++, using a RTree for storing my data. The algorithm works fine if I process up to 5 points then if I try 6 points it gives a segmentation fault.
Using gdb I have discovered that the problem is here:
Program received signal SIGSEGV, Segmentation fault.
std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (
this=0x7fffffffd7c0, __str=
<error reading variable: Cannot access memory at address 0xffffffffffffffe8>)
at /usr/src/debug/gcc-4.7.2-20120921/obj-x86_64-redhat-linux/x86_64-redhat-linux/libstdc++-v3/include/bits/basic_string.tcc:175
175 __str.get_allocator())
Can anybody help me to understand where the error is or ar

Mitch Wheat already suggested this in a comment, but I think it is in fact the best answer for your problem, so:
Use valgrind.
Valgrind is a tool (or rather, a set of tools) for which the default mode of operation is to run your program and check for memory errors, such as leaks, buffer overruns, uninitialized reads, and more.
Literally all you need to do is to build your program, preferably with the -g option to make it easier to debug, and then run it like valgrind my-prog args.... Valgrind will then print out detailed error reports with stack traces when something bad is going on. Your problem here will likely be found without ever needing to use a regular debugger, nor have us guess at the problem (if this were my own problem, I'd use valgrind too).

Given only that, my best guess is that you're trying to create a string using invalid source data, possibly a char pointer that's unitialized or that's pointing to a string that's already been freed

Related

What does the message following error message in visual studio mean? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
So I am creating a multi-threaded C++ program and it returns this error:
Unhandled exception at 0x77e915de in HANASimulationSync.exe:
0xC0000005: Access violation reading location 0x00000020.
By the way, when I run this code in a single thread it doesn't return any errors. I'd just like to know what sort of error is this? Does it have something to do with memory management?
It means that you attempted to read from an invalid memory location. In this case the location was 0x00000020. Since that is just a small offset above zero, it seems that you are probably de-referencing an offset from a null pointer.
So, a common way to see something like this would be if you had a struct with a member at offset 0x20. And then if you tried to read that member from a pointer to a struct where the pointer was null. And you would get the same error indexing into an array using a null pointer. I think you get the idea now.
The other relevant information is the code address which led to the fault. In your example that is 0x77e915de. If you configure your linker to produce a full image map you'll be able to identify which line of code led to the fault. Even better, if you produce debug information then you can attach a debugger.
In general terms this sort of error is known as a segmentation fault, or an access violation, or a protection fault. Those terms all mean the same thing.

g++,How to find where there is a error? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Guys!
I have written a c++ source.
But I find someplace there is a error.
"Segmentation fault: 11"
But I don't know the details about the error and where to fix?
Do you guys knows some commands can show where the error is.
Thanks
With gcc generated code you typically use a gdb-based tool or gdb itself. Just run
gdb <program>
... and then inside gdb:
run <arguments>
... and it will stop where the crash happens.
To get reasonable information about the program location you want to compile with debug information,i.e., using the -g option. To avoid confusion you might want to compile without optimization, i.e., without any -O... option. However, some errors are only triggered when optimization is turned on (note: these are generally still errors in your code and not in the optimizer).
Use the GNU Debugger (GDB). Add break points and run the code. Here is where you can find stuff. http://www.unknownroad.com/rtfm/gdbtut/gdbtoc.html
It sounds like your program is trying to access an invalid (non-existent) address. It is also possible that it's trying to access misaligned data. I've seen this before when attempting to access misaligned structures.
Read up on segmentation faults: http://en.wikipedia.org/wiki/Segmentation_fault

Segmentation fault. 0xb7d9e768 in memmove () from /lib/libc.so.6 [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
GDB gives me the above error WRT my C++ program. Nowhere I have used any memory function including new and delete etc.
I want to understand the 'MEANING' of this error.
If run your program under gdb, you should be able to print out the backtrace and see what part of your code is causing the segmentation fault. memmove() may being called indirectly through a different system call.
It is possible that an array operation in your code gets optimized as a call to memmove: this is probably why the compiled code uses memmove, whereas your source code doesn't.
I think you should check that you are not accessing your arrays out of bounds.
memmove tried to access (read or write) a memory segment it shouldn't touch.
The reasons can be manifold, but probably pointer corruption. Check it with a debugger, valgrind, check stack trace, etc...

C++: More and more complex bugs misleading the debugger [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 11 years ago.
It seems like the bugs in my project are evolving. While in the beginning time, causers of program crashes were easily to spot using the debugger (the line it points to after the program crashes), it's different now.
Many bugs cause the program to crash in completely arbitary places not even closely related to them.
How can I spot bugs "hiding" from the debugger in a better way?
(I use Visual Studio 2010)
Profile your code for memory corruption, memory overwrites and memory leaks.
Root cause analysis.
When you find an obvious bug, don't just fix the bug, fix the coding style that allowed it.
If you have any code using raw memory and pointers, replace it by memory allocated using std::vector and its iterators. It will compile to exactly the same speedy code in Release mode, but in Debug mode it will used checked iterators, which will catch some bugs as early as possible.
Use memory corruption checking utilities, like gflags or debug heap. "Floating" crushes almost always come from corrupted memory in C++ programs

How to fix heap corruption in c/c++? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 12 years ago.
This is a further question for my previous one here.
Is there a general solution for this?
Fix all dangling pointers
Fix all buffer overflows
Use pointers only where they are really needed
Reading your original post I'm not 100% you are facing a heap-corruption and you really hope you don't because if you would this is one of the most tricky errors to track down and AFAIK there are no solutions that always work.
However, if you are positive its a heap-corruption and you are on the windows platform you could try out the tool gflags. This advanced debugging tools allow you to install a debug heap manager in order to make it possible to find certain kinds of heap corruptions. One of the neat options it has is to make each allocation in it's own page or to write protect the heap datastructures. This makes it easier to pinpoint the exact location of the heap-corruption. It sucks lots of memory and CPU though.
Another tip if you are using Visual Studio is that if you manage to find that something is corrupting the data after a certain point of time and you wish to find the code that corrupts the data you can use Data Breakpoint to break whenever someone changes the data in question.
I wish you good luck.
Adding to swegi's answer I would check all your C++ constructors and all C initializations. Transform all dynamic C++ constructors (those where you put the init code in the function body) into static ones (where you initialize with the special constructor syntax). And be sure that you init all pointers and pointer members to 0. For C, I would initialize all variables.
Then, on unix I would use valgrind, usually this is quite good in finding all access violations and if you compile with all debugging options on it is able to trace it to the source line. There should be something similar on windows, too.