1
votes

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;
2
we need some code to work off of, seg faults can happen for a number of reasons - Syntactic Fructose
cut code off until you have ~20 lines. Post those here. - Luchian Grigore
Make sure you do std::cout << "Check" << std::flush; otherwise you're not guaranteed to get any output. - Joseph Mansfield
I am not psychic Please help with a LITTLE code. - Ed Heal
You could also try running your code under valgrind. There's a possibility that you're corrupting something in one place, and then then accessing the corrupted memory in a way that generates the segfault, such as dereferencing junk as a pointer. - Phil Miller

2 Answers

0
votes

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.

0
votes

Valgrind is a useful tool for debugging a program. At the first view I don't see a initialization for world variable.