I've got a class with a simple function for returning an entry found in a vector.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12
class A
{
public:
B* getB()
{
return bees[index];
}
private:
unsigned index;
vector< B* > bees;
};
When I run this function, I get a segfault. (gdb traces the segfault to this function) "Okay," I say to myself. "Must just be because index is out of bounds somehow."
Wrong answer. Adding a check that index is less than bees.size() gives me a segfault in the size function, on the begin iterator.
The function is not trying to return a null pointer or some such thing, all 'B*'s pass a functionality test with flying colors before being pushed into the vector.
Nuh uh, the code that handles these objects looks something like this:
1 2 3 4 5 6 7 8 9 10 11
B* b = new B();
// Various methods of b are run, all working without any problems.
A* a = new A();
a->addB( b ); // 'addB' is just 'bees.push_back( b );'
B* tempB = a->getB(); // (with index = 0) Segfault.
tempB->doMethod(); // Doesn't happen.
Oh wait...
Okay, that's embarrassing. I just recently added a pointer as a member of the B class, and forgot to implement a copy constructor... Good job wasting three hours, self.