简体   繁体   English

关于push_back的Segfault

[英]Segfault on push_back

In piece of code below, I get a segfault on the line calling push_back(): 在下面的代码中,我在调用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 . 如果从代码中不清楚,我试图初始化一个对象trigger将它推到列表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? TriggerManger在哪里被使用和声明? 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. 我打赌,因为它在push_back上崩溃,CTriggerManager的实际实例为null并且第一次访问成员变量(m_Triggers)导致访问冲突。

I see one declared here, but never instantiated: world.cpp: 我看到一个在这里宣布,但从未实例化: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. 问题显然不在这个函数中,甚至不在TriggerManager.h / cpp文件中 - 这两个类都非常简单和天真。 btw, to initialize m_EventName better to use initalization list in constructor, not an assign operator... 顺便说一句,更好地初始化m_EventName以在构造函数中使用初始化列表,而不是赋值运算符...

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 ... 尝试使用valgrind例如......可能会显示你对真正原因感兴趣... - 在调用AddTrigger之前,你似乎在某处损坏了内部列表...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM