【发布时间】:2009-09-17 05:58:02
【问题描述】:
我的班级有这个成员。
std::vector<AvaWrapper> m_controls;
在我的构造函数中调用
m_controls.clear()
然后我调用了一个成员函数,该函数再次执行 m_controls.clear() 但它因断言而崩溃。调试器显示 m_controls 有 50 万个或更多条目,尽管它们都不是有效的,因为当我展开树时调试器显示“错误:无法评估表达式”。所以,我的直觉是这个类没有正确创建,因为这段代码确实有效,但我后来从这个类派生了一个类,我调用 new() 来创建父类。在它作为基类的新角色中,它正在爆炸。然而,this 指针显示所有其他成员变量都有有效数据,所以我的预感是错误的。构造函数也被调用。有任何想法吗?谢谢。
更新2:
Train::Train() : SpriteWindowFrame(200)
{
s_mutexProtectingTheGlobalData = new wxMutex();
m_window_rect = NULL;
m_thread = NULL;
m_ok = false;
m_accumulate_timer = new wxTimer();
m_accumulate_timer->SetOwner(this, ACCUMULATE_TIMER_ID);
m_autohide_timer = new wxTimer();
m_autohide_timer->SetOwner(this, AUTOHIDE_TIMER_ID);
m_autohide = false;
m_autohide_period = 5000;
m_controls.clear();
}
更新:
//This version works.
SpaceInit::SpaceInit()
{
//Use INI config store. If you need something else, just
//create the appropriate object.
m_config_store = new IniConfigStore();
//Start up config.
Init();
m_t = new Trains();
return;
}
SpaceInit::~SpaceInit()
{
wxDELETE(m_config_store);
return;
}
我可以这样做: m_t->SomeMemberFunctionThatManipulatesVector()
它有效。
这个没有
SpaceInit::SpaceInit():Trains()
{
//Use INI config store. If you need something else, just
//create the appropriate object.
m_config_store = new IniConfigStore();
//Start up config.
Init();
return;
}
我做不到: SomeMemberFunctionThatManipulatesVector()
在矢量上爆炸。
我刚刚注意到 this 指针在 Train() 默认构造函数中确实搞砸了。我以为不是,但它是。 Trains 构造函数运行 但一切都被破坏了。
我的 Trains 构造函数代码运行正常。只需初始化一些东西,新建一些东西,等等。 SpaceInit 是用 SpaceInit* t = new SpaceInit(); 创建的。 Train 是一个派生类,所以可能与它有关?
【问题讨论】:
-
能否给我们头文件和所有相关代码?
-
std::vector 的模板参数是什么?这可能会给我们一个线索。
-
你需要给我们更多的代码。您的简化还不够,而且是错误的。您不可能拥有成员
std::vector m_controls;,因为这不是vector的有效用法。 -
我会尝试发布更多代码,但 GMan 我不知道你在说什么。如图所示,我有大量使用矢量成员函数的类。
-
为什么人们认为他们对问题的英文描述比源代码更能激发程序员解决问题。