【发布时间】:2014-06-14 23:12:18
【问题描述】:
我有一个 list 或 unique-ptrs,在清除列表后,会出现以下错误:
Unhandled exception at 0x013EA350 in Last.exe: 0xC0000005: Access violation reading location 0xFEEEFEEE.
当迭代元素时,这在 std::list 文件中触发。这个问题有点独特:
-
MenuManager有一个unique_ptr<Menu>对象列表。 -
Menu有一个unique_ptr<MenuItem>对象列表。 - 第一个
Menu及其所有MenuItems完美破坏,但是在清除MenuManager列表中的第二个Menu时触发此错误,与完全独立的MenuItems。看起来第二个unique_ptr<Menu>在第一个销毁后变得未定义,尽管它在MenuManager的销毁开始时存在且非空。
这是什么原因造成的? (具体来说,不是我的代码在哪里导致了这个错误。我想知道这可能是什么原因)
MenuManager.h
class MenuManager
{
public:
/*MenuManager(list<Menu*> menus)
: m_menus(menus) { SetMenu( menus.front() ); }*/
MenuManager() : m_currentMenu(nullptr) {}
// Get the menu that the manager is pointing to.
Menu* GetCurrentMenu(void) { return m_currentMenu; }
// Set the current menu based on its name.
void SetMenu(string menuName) { SetMenu( GetMenuByName(menuName) ); }
// Add a menu to the list of menus managed by this object.
void AddMenu(Menu* menu);
// Render the current menu.
void RenderMenu(void) { m_currentMenu->Render(); }
private:
list< unique_ptr<Menu> > m_menus; // The list of menus managed by this object
Menu* m_currentMenu; // A pointer to the menu in current use
Menu* GetMenuByName(string menuName);
void SetMenu(Menu* menu); // Set the menu based on the menu argument
};
Menu 是另一个基本上不相关的类的子类。但是,它的列表定义为list< unique_ptr<MenuItem> >。 MenuItem 的定义无关紧要。
【问题讨论】:
-
请出示代码。听起来您描述的代码中存在错误。
-
请贴出
Menu和MenuItem的定义 -
好的,会的。虽然真的没有比这更多的了。
-
很公平,在这个问题上 +1 可以抵消其他人的不耐烦。您发布的大部分内容都是声明,而不是定义。如果您可以将此问题缩小到足以发布的小问题(但它是一个独立的可编译程序),但仍然表现出症状,我相信我们可以诊断它。
-
0xFEEEFEEE 表示内存已被释放。 stackoverflow.com/questions/127386/…
标签: c++ list c++11 unique-ptr