Reputation: 5389
I have the following class definition:
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;
};
The implementation of the class is:
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()
{
}
The addDisplay()
method is called by another class like:
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);
}
In addDisplay()
method, when I do mDisplay = &display;
, I get the following exception:
Unhandled exception at 0x75A818A2 in My_Executable.exe: Microsoft C++ exception: std::runtime_error at memory location 0x0073F66C.
The application is working as expected despite this exception. I can't quite figure out the reason for this exception.
EDIT 1:
I tried to put a try-catch:
try
{
mDisplay = &display;
}
catch(const std::exception&)
{
// Couldn't reach this code.
}
When I tried this, strangest thing happened. I get the exact same exception and it is raised at the line that contains try
...
EDIT 2:
Link to documentation on al_create_display(). It returns a raw pointer to a display.
EDIT 3:
I suspected that it's the assignment operator that is causing the issue. So I tried replacing my ALLEGRO_BITMAP *mDisplay
with a std::vector<ALLEGRO_BITMAP *>
and instead of doing mDisplay = &display
, I did mDisplay.push_back(&display)
. Now, the exception disappeared. I really appreciate it if anyone can shed more light into it. May be the copy assignment operator is disabled?
Upvotes: 0
Views: 5255
Reputation: 243
hope this can help you see the comment below:
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
}
look at where we call it
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
Upvotes: 1