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
Possibly you are looking at the wrong server class. You create an instance of CServer
while the class definition you are showing is for a class Server
. (Alternatively this might also be a typo in the question.)
Also, if you hit the keyboard too fast, before the new thread is created and the ServerHelper
function is run, you might execute the delete
before the server is created. delete
will then just see a null-pointer and do nothing, the real server object that is created later won't be destroyed.
Using these class definitions (and the rest identical to what you posted)
class TimeManager
{
public:
virtual ~TimeManager() { cout << "~TimeManager" <<endl; }
};
class UserManager : virtual public TimeManager
{
public:
virtual ~UserManager() { cout << "~UserManager" <<endl; }
};
class CServer : virtual public UserManager
{
public:
virtual ~CServer() { cout << "~CServer" <<endl; }
};
Running display
~CServer
~UserManager
~TimeManager
between the first & second times I press enter --- Exactly as one would expect. It seems your problem is elsewhere.
NOTE also, that there are a number of typos in the CServer class, notably, it sometime "CServer" and other times "Server". Also, "virtual" is spelled wrong, and the dtor is private. But any of those would have prevented it from compiling, not caused a run-time error.
NOTE also, that the code, as you posted it, does not need virtual inheritance. You are either needlessly tossing the keyword around, or your classes are more complex than you are letting on.
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:)...
is CServer some other class not defined by you? your class is defined as Server, but you're creating a CServer.
So I tried compiling this code and it doesn't compile, even after fixing the typos. I fixed the typos and made your Server destructor public and everything works as I would expect. the Server destructor is called first, UserManager second and TimeMangager third.
精彩评论