Search code examples
c++visual-studio-2010destructordelete-operator

C++ Inheritance: Destructor does not get called


I got my code like the following:

class TimeManager
{
public:
 virtual ~TimeManager();
};

class UserManager : virtual public TimeManager
{
public:
 virtual ~UserManager();
};

class Server : virutal public UserManager
{
 virtual ~Server();
};


CServer *pServer;

DWORD WINAPI ServerHelper(void*);

int main()
{
 //Create server
 CreateThread(NULL, 0, ServerHelper, NULL, 0, NULL);

 std::cin.get();

 //delete server
 delete pServer;

 std::cin.get();

 return 0;
}

DWORD WINAPI ServerHelper(void *v)
{
 pServer = new CServer;

 return 0;
}

My Problem is - guess - that my Server destructor won´t get called...

I can´t imagine, why:/... (I wrote output functions into all three classes and the server constructor does not output anything, but both of the other does... right after the SECOND! key-hit... (why the second and not right after the deletion?)

Any hints, tips, solutions?....

I am using visual studio 2010


Solution

  • Ok... I found the problems´ solution:

    I started the main loop from within the Servers constructor:

    Server::Server()
    {
    MainLoop();       // <- Loop in there...
    }
    

    I fixed it, by starting the server manually via an extra function and eveything is fine now :D...

    Thank You to everyone who participated:)...