Any method calls on an std::list resulting in a segfault.

Hello everyone. I am having a super weird problem.

I'm using std::list to keep track of all the actions an "Actor" should take. It doesn't really matter what the list is for, but just for clarity it's being declared like this in Actor.h:

std::list<Action::Ptr> _actions;
Where Action is a simple class I wrote, the specifics of which also don't really matter.

The problem is, the list of actions is working just the way I want it to in most cases. For example, the following methods in Actor.cpp work just fine when called:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void Actor::pushAction(std::string name, std::string location)
{
    // Check to make sure the user specified a valid location for insertion.
    if(location != "Top" && location != "Bottom")
        throw fwk::InternalException("Must name either 'Top' or 'Bottom' as the location for pushing an Action.");

    // Check to make sure the user specified a valid action.
    if(!actionName(name))
        throw fwk::InternalException("This Actor does not know the action: " + name);

    if(location == "Top")
    {
        // If the current action should terminate, terminate it.
        if(!_actions.empty() && peekAction()->terminates())
            endAction();

        _actions.push_front(new Action(name));
        _actions.front()->execute();
    }
    else
        _actions.push_back(new Action(name));
}

Action::Ptr Actor::peekAction(std::string location) const
{
    if(location != "Top" && location != "Bottom")
        throw fwk::InternalException("Must name either 'Top' or 'Bottom' as the location for peeking an Action.");

    if(location == "Top")
        return _actions.front();
    else
        return _actions.back();
}


That all works, which is fine and dandy. Just under those methods, I have the following method which results in a segfault if I call any method at all on _actions.

1
2
3
4
5
6
7
8
void Actor::printActions()
{
    std::list<Action::Ptr>::iterator itr;
    for(itr = _actions.begin(); itr != _actions.end(); itr++) // currently segfaulting at _actions.begin()
    {
        std::cout << (*itr)->name() << std::endl;
    }
}


Even just doing something like:
 
if(_actions.empty())

causes a segfault.

If anyone has any ideas, I'd be very appreciative!

It sounds like you've had a memory overwrite somewhere and smashed _actions.
Interesting. I wonder how I managed to do that. What kinds of things can I look for in my code to see if that is the case?
I agree with @kbw, you've got memory corruption somewhere else.

For your "Top" and "Bottom" thing, shouldn't this be an enum class? It would make it impossible to give an invalid parameter like "Side", and it would be more efficient as you're not performing a string comparison all the time nor passing strings by value (though C++11 move semantics generally optimize pass-by-value).
L B, yes, definitely. Honestly I just used the top and bottom nonsense because I was throwing it together so quickly and wasn't quite sure whether those methods were in the right class or if I would be moving them, so I just wrote them the quickest way I knew how. But that is good advice, and it will be taken once I figure out this memory issue.

The code that I posted is actually from a library that I'm writing, but I feel the problem is probably stemming from the program I've written to test the library. We'll see. I will probably not have time to look at the code again until after the weekend, but I will post whatever answer I come up with if it's worth reading.

Thanks for the help, as always, folks.
Topic archived. No new replies allowed.