bug-commoncpp
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pure Virtual Method call problem. (Bug in library?)


From: David Sugar
Subject: Re: Pure Virtual Method call problem. (Bug in library?)
Date: Tue, 15 Apr 2003 09:55:49 -0400
User-agent: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.3) Gecko/20030312

The C++ standard does not permit virtual methods to be called from constructors or destructors. In many cases older compilers had ignored this, since often the virtual table is populated anyway before the constructor is called, but that is implimentation specific behavior and should not be relied upon. Hence, things that can invoke virtuals cannot be specified in constructors or destructors either.

Jon wrote:

I keep getting a runtime error when using the following code. As you can see, both of the derived classes have their own run() method, however, the base class defines this method as virtual and required for all subclasses. The problem occurs when start() is called in the constructor of the derived class. I get a pure virtual method call runtime exception and the program dies. This is compiling under gcc3.2 in Linux, however, in windows MingW with gcc3.2 everything is fine!!! Since I have no compiler directives and the same compiler on each system, I cannot see what could be the problem.

Is this a difference in the port of gcc? Or is it caused by the library calling run() from the start() method call? I am using 1.0.9 without modification on Linux, however, I had to comment HAVE_OLD_IOSTREAM on windows.

I can provide the full source code if necessary


#ifndef SERVER_H
#define SERVER_H
#include "thread.h"
#include <cc++/thread.h>
#include <cc++/socket.h>
#include <list>
#define DEFAULT_WITHDRAW_PORT 1234
#define DEFAULT_DEPOSIT_PORT 5678
using namespace std;
using namespace ost;
class Server:public Thread{
   public:
       Server(){
               cout << "SERVER" << endl;
               setCancel(Thread::cancelImmediate);
       }
       ~Server(){
               cout << "~SERVER" << endl;
               terminate();
               delete sock;
               delete addr;
       }
       virtual void run()=0;
       static void setHost(char* host){
           Server::host=host;
       };
       static void setPorts(int withdraw,int deposit){
           withdraw_port=withdraw;
           deposit_port=deposit;
       }
       static Server* withdraw;
       static Server* deposit;
   protected:
       InetAddress* addr;
       static char* host;
       static tpport_t withdraw_port;
       static tpport_t deposit_port;
       TCPSocket* sock;
};
class WithdrawServer:public Server{
   public:
       WithdrawServer(){
           this->port=Server::withdraw_port;
            start();
       }
       void run();
   private:
       tpport_t port;
};
class DepositServer:public Server{
   public:
       DepositServer(){
           this->port=Server::deposit_port;
            start();
       }
       void run();
   private:
       tpport_t port;
};
#endif
//////////////////////////////////////////////////////////////////////////////////////////
char* Server::host;
tpport_t Server::withdraw_port;
tpport_t Server::deposit_port;
Server* Server::withdraw;
Server* Server::deposit;

void WithdrawServer::run(){
   if(!Server::host)return;
   addr=new InetAddress(Server::host);
   sock=new TCPSocket(*addr,port);
   while(true){
       if(sock->isPendingConnection()){
           cout << "W Connected" << endl;
           new WithdrawThread(*sock);
           cout << "Added to list" << endl;
           yield();
       }
   }
}
void DepositServer::run(){
   if(!Server::host)return;
   addr=new InetAddress(Server::host);
   sock=new TCPSocket(*addr,port);
   while(true){
       if(sock->isPendingConnection()){
           cout << "D Connected" << endl;
           new DepositThread(*sock);
           yield();
       }
   }
}



_______________________________________________
Bug-commoncpp mailing list
address@hidden
http://mail.gnu.org/mailman/listinfo/bug-commoncpp






reply via email to

[Prev in Thread] Current Thread [Next in Thread]