views:

144

answers:

5

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 destrcutor 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

+2  A: 

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.

sth
it is just a typo...the server class is not too big and got an logger which tells me that everything is set up... and running...even if i wait a minute and press, wait a minute again, and press the second stroke... it does not get called...
Incubbus
A: 

is CServer some other class not defined by you? your class is defined as Server, but you're creating a CServer.

Chris
It is the same... as above its just a typo...
Incubbus
A: 

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.

Chris
Omg... Do You really think i did copy this code from above from my real code?... I just written it by hand into here... OF COURSE: constructor ), there is a std::cout in it... PLease dont do multiple answers... write comments --------> "Add Comment"
Incubbus
well if you want actual help you should post code that works in the future. after i fixed the code it works as expected, so i'm not sure what's wrong with your code, especially since i don't know what your "real code" looks like. good luck.
Chris
@incubbus: People will help you with the code you post. If you post code that doesn't compile, then it makes our task harder. Make your examples simple, sufficient, and correct, and you'll get better answers.
Bill
I found the answer... posted it in another answer to this question...thx for your help.
Incubbus
+1  A: 

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.

James Curran
they are more complex... it is way too much code to post it all...
Incubbus
@Incubbus do you flush the stream like in the code above, with endl?
Maciej Hehl
A: 

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:)...

Incubbus