Microsoft C++异常runtime_error在内存位置(代码 0x75A818A2)
Microsoft C++ exception runtime_error at memory location (Code 0x75A818A2)
我有以下类定义:
class DisplayManager
{
public:
static DisplayManager *getInstance();
DisplayManager(DisplayManager const&) = delete;
void operator=(DisplayManager const&) = delete;
~DisplayManager();
void addDisplay(ALLEGRO_DISPLAY &display);
private:
DisplayManager();
ALLEGRO_DISPLAY *mDisplay = nullptr;
};
该类的实现是:
DisplayManager *DisplayManager::getInstance()
{
static DisplayManager instance;
return &instance;
}
DisplayManager::~DisplayManager()
{}
void DisplayManager::addDisplay(ALLEGRO_DISPLAY &display)
{
if(!mDisplay)
{
throw std::runtime_error("Failed to create display: A display is already created.");
}
mDisplay = &display;
}
DisplayManager::DisplayManager()
{
}
addDisplay()
方法由另一个类调用,如下所示:
void Display::createDisplay()
{
auto *manager = DisplayManager::getInstance();
if(!manager)
{
throw std::runtime_error("No diplay manager.");
}
ALLEGRO_DISPLAY *display = al_create_display(width, height);
if(!display)
{
throw std::runtime_error("Failed to create display");
}
manager->addDisplay(*display);
}
在addDisplay()
方法中,当我执行mDisplay = &display;
时,我得到以下异常:
Unhandled exception at 0x75A818A2 in My_Executable.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0073F66C.
尽管存在此异常,应用程序仍按预期工作。我不太清楚这个例外的原因。
编辑 1:
我试图尝试捕捉:
try
{
mDisplay = &display;
}
catch(const std::exception&)
{
// Couldn't reach this code.
}
当我尝试这个时,最奇怪的事情发生了。我得到完全相同的异常,它在包含try
的行中提出......
编辑2:
链接到有关 al_create_display() 的文档。它返回指向显示器的原始指针。
编辑3:
我怀疑是分配运算符导致了问题。所以我试着用std::vector<ALLEGRO_BITMAP *>
代替我的ALLEGRO_BITMAP *mDisplay
,而不是做mDisplay = &display
,我做了mDisplay.push_back(&display)
。现在,例外消失了。如果有人能更多地了解它,我真的很感激。可能是复制分配运算符被禁用了?
希望这可以帮助您查看下面的评论:
void DisplayManager::addDisplay(ALLEGRO_DISPLAY &display)
{
if(!mDisplay)
{
throw std::runtime_error("Failed to create display: A display is already created.");
}
mDisplay = &display; // dangerous to do this, mDisplay point to display
//if display is being destroy => mDisplay point to un-legal memory
}
看看我们在哪里称呼它
ALLEGRO_DISPLAY *display = al_create_display(width, height); // problem in here
//if this function return object which will being destroyed later, then error happen
if(!display)
{
throw std::runtime_error("Failed to create display");
}
manager->addDisplay(*display); // look at this, when we are out of this
//and the object above is being destroy, mDisplay point to un-legal memory => exception
相关文章:
- 如何使用 lldb 查找我的代码运行的位置
- 在C++代码中搜索对象的实例化位置
- 我应该在服务模板中的什么位置添加自己的代码?
- 在 C 代码中调用 Python 函数时第三次出现访问冲突写入位置错误
- 当光标位置在编辑控件 MFC 中更改时是否有通知代码?
- C++11 中的随机数:有没有一种简单的方法可以将生成器种子放在代码的一个位置,然后在不同的函数中使用它?
- cv:内存位置的异常 - 运行 openCV 代码 Visual Studio 2013 & Visual Studio 2017 - Windows 7
- 我如何在代码中找到(和删除)需要管理员特权的位置
- 如何跟踪通过外部代码修改类变量的所有位置
- 了解现代OpenGL中的代码绘图.OBJ并设置不同的位置
- Microsoft C++异常runtime_error在内存位置(代码 0x75A818A2)
- 给定代码段中引用的时间和空间位置的所有出现
- 当我在Xcode上访问矩阵(openCV垫子)的位置时,为什么我会得到未定义的行为(exc_bad_access(代码=
- 8谜题代码.我的队列没有填充,我找不到错误的位置
- 在C++中,为什么代码示例在重载运算符时经常使用内存位置
- 是否可以从代码中获取依赖库的位置
- 如何在C++中获取代码片段的位置
- G -FPIC不产生位置独立代码
- 如何在G 代码中找到给定的成员函数或CTOR的所有位置
- 在 Win32 中的位置C++代码放置 SetMenuItemInfo() 以根据焦点启用/禁用