Weird segfault with std::malloc - c++

I'm getting a segfault with the following line of code:
char* addr = (char*)std::malloc(bytes);
When running GDB I see that bytes has a value of 851984. As far as I know that shouldn't be a big deal. I can't seen anything wrong with it for the life of me. Anybody have some suggestions?
The actual segfault error is:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff674dd75 in malloc_consolidate (av=0x7ffff6a87720) at malloc.c:4254

There is (most likely) an issue somewhere earlier in your code which is corrupting heap structures, causing malloc to fail on allocation. You should try using a memory error detection tool like valgrind to debug this issue.

Related

How to map the information in a Segmentation fault to the line in the source code?

I get a Segmentation fault (Address not found). The outcome I receive looks something like this:
#3 Object "/some/dir/bin/myProject", at 0x415526, in
#2 Object "/some/dir/bin/myProject", at 0x40d3f9, in
#1 Object "/some/dir/bin/myProject", at 0x409b1a, in
#0 Object "/lib64/libpthread.so.0", at 0x7f9a82a78cd0, in pthread_mutex_lock
I would like to know to which command the positions translate in myProject.cpp. How can I do that?
The reason why I do not examine this with gdb is that this error is caused by the way multiple threads communicate and human intervention introduces so much wait time that this error does not happen. The error is also sporadic...
Additional question:
My understanding of the error Address not found is that this is probably caused by dereferencing a nullprt or when trying to access a container at the wrong place. Would you agree?
EDIT
Thank`s for suggesting to debug the core dump files. The problem with this is: When there is this error, the program does not crash! The error (or a similar one) is printed to console and the program gets stuck (multithreading again...). It does not terminate, so I guess that there are no dump files (correct me if I am wrong!). I could not find any...

How to track a invalid pointer in C++?

I have my code ready to run with PROOF.
Whenever I run code standalone I works fine, when I activate PROOF, my code crash with a Segmentation Fault.
With GDB I'm able to know exactly where it crashes: when I try to follow a pointer to a object. This makes me think that this pointer (that I'm absolutely sure it was valid before) is invalid and I have no idea why so.
Are there other options? can I track that pointer so I know where it was released?
Use valgrind memcheck tool with --leak-check=summary --track-origins=yes.
This shows invalid memory access (Segmentation Fault) and where they had been freed.

program received signal SIGSEGV, segmentation fault

I am running a program in multi threaded environment it fails at some point. when i tried it with gdb than it is shoing the following error.
program received signal SIGSEGV, segmentation fault.
[switching to thread 0x7fff677b700 (LWP 2777)] 0x00007ffff7aa42b9 in
process_incomplete_rows (resultset=0x507950) at c/mgmt.c:479 479
c/mgmt.c: No such file or directory.
mgmt.c file is there and this code is working fine for some options but 2 or three options its giving this error. What could be the cause of this error. Its error in comiplation or in coe? or its error while accessing some data?
A segmentation fault is a runtime error that is usually due to referencing an invalid pointer. Usually that invalid pointer has never been initialized, but sometimes it is reusing an old pointer or writing past the end of an allocated memory chunk (such as past the end of a string).
It probably means that your node variable is either NULL or corrupted. Run your program in the debugger, stepping through it from some point prior to the crash up until it and see where you've messed it up. Or use one of the automated tools like Purify or Insure++ to track it down for you.

How To Determine Seg Fault?

I have an interesting Segmentation Fault. It occurs at an unknown place in my code. The code is fairly simple, two objects, and one general function. The function is supposed to create a graph of the objects. When I run the code with just a main calling the function, I get a seg fault and the following line of code in GDB.
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff758a02c in free () from /lib/x86_64-linux-gnu/libc.so.6
When I add a line into the main right before the function call that is simply cout << "Check"; I still get a segmentation fault, but the check does not appear in output. Really lost here. What should I try next?
EDIT:
Thanks for the help with using flush. I've found the area in the code that's causing the seg fault. The functions I'm using are new to me though so I'm still a little lost. Anyone see a bug?
const char* inFile = inFileP.c_str();
list<CContinent> world;
CCountry *homeCountry = new CCountry;
CCountry *neighborCountry = new CCountry;
fstream filestr;
filestr.open(inFile, fstream::in | fstream::out | fstream::app);
string line;
From painful experience, when a crash happens in malloc or free it is because of heap corruption. Any of the usual suspects can cause heap corruption - allocate 10 bytes, write 11 - free, then write to free'd memory, double free, et.al.
Valgrind is a useful tool for debugging a program.
At the first view I don't see a initialization for world variable.

Possible reasons for segmentation fault on function exit

What possible reasons can you think of for a segmentation fault on exiting a function in C++? What I mean by that is, that I have a reproducible segmentation fault in a C++ program, and when I investigate using GDB is says
Program received signal SIGSEGV, Segmentation fault.
FooBar (bla=...) at foo.cpp:59
59 }
where the indicated line contains the closing bracket of my function.
There could be many reasons of this. Run program under Valgrind and most likely it will tell you exact reason or at least will help to investigate and narrow down the problem.
It's quite likely a buffer overrun on some buffer located on your stack. This overwrites the return address, so when your code tries to return to the previous stack frame, it instead jumps to some random address which is more likely than not non-executable, so you get a segmentation fault.
But without seeing some more code or more information about the crash, it's impossible to say what the exact cause is.
My first guess is the destructor of a class is freeing an invalid pointer.
in my case I had an std::thread that had not been properly joined.