Why can't I delete the Ogre OverlaySystem created with new?

2

I am using the Ogre framework for my application. When the application shuts down I shut down Ogre deleting the Ogre::Root object. After that I need to delete some pointers, because I have own classes for every Ogre class I use as interfaces. I just delete the pointers to my classes. The pointers that existed to Ogre elements are not deleted since the Ogre shut down takes care of it (or at least it should according to the documentation).

The problem lies within the OverlaySystem class. This is the header file:

class OverlaySystem {
public:
    OverlaySystem();
    Ogre::OverlaySystem* getOverlaySystem() const;
    ~OverlaySystem();

private:
    Ogre::OverlaySystem* overlaySystem;
};

and this is the source file:

OverlaySystem::OverlaySystem() :
overlaySystem(new Ogre::OverlaySystem()) {
}

Ogre::OverlaySystem* OverlaySystem::getOverlaySystem() const {
    return this->overlaySystem;
}

OverlaySystem::~OverlaySystem() {
    delete this->overlaySystem;
}

My understanding is as follows: since I dynamically allocate memory for the Ogre::OverlaySystem object I have to delete the pointer freeing the allocated memory, which is what I do in the destructor (or at least I thought I would).

The problem is: when my application shuts down I get an exception deleting the Ogre::OverlaySystem object because of an access violation saying that I can't read at position 0x00000042. Here is an example of the failure (sorry for german but I can't change that right now):

Access Violation Exception

The only address changing is the exception address itself, which is the first one.

I spent hours now searching for my failure or the reason for this but I can't find anything helping. It would be really great if someone could help me solve this problem.

c++
pointers
access-violation
ogre
asked on Stack Overflow Feb 24, 2016 by Lilo

1 Answer

0

Found the error! I thought I had done that already but there must habe been some mix up after several changes.

Solution: The destructor of Ogre::OverlaySystem must be called BEFORE the destructor of Ogre::Root is called.

answered on Stack Overflow Feb 25, 2016 by Lilo

User contributions licensed under CC BY-SA 3.0