سؤال

In piece of code below, I get a segfault on the line calling push_back():

CTrigger CTriggerManager::AddTrigger(const std::wstring& eventName)
{
    CTrigger trigger(eventName);
    m_Triggers.push_back(trigger);
    return trigger;
}

In case it is not clear from the code, I am trying to initialize an object trigger push it onto the back of a list m_Triggers. Can anyone please tell me why this might cause a segfault?

If needed, see the code in full context here.

هل كانت مفيدة؟

المحلول

Where is the TriggerManger being used and declared? I am betting that since it is crashing on the push_back, that the actual instance of CTriggerManager is null and the first access into a member variable (m_Triggers) is causing an access violation.

I see one declared here, but never instantiated: world.cpp:

CTriggerManager* pTriggerManager = NULL;

نصائح أخرى

the problem obvoiusly not in this function and even not in TriggerManager.h/cpp files -- both classes are really simple and naive. btw, to initialize m_EventName better to use initalization list in constructor, not an assign operator...

Try to use valgrind for example... may be it will show you smth interesting about a real reason... -- it seems you corrupt an internal list somewhere before call to AddTrigger...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top