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: Sat, 19 Apr 2003 17:09:26 -0400 (EDT)

That is not surprising.  I have found even stranger wierdness on macosx.  
I am at the moment of the belief that the dynamic loader is broken for c++ 
shared libraries on macosx.  That particular problem is best illustrated 
in the tests/ programs; digest runs while url1 freezes.  I found that in 
the case of digest that the ccgnu2 constructors for library based static 
objects ran, while in the case of url1, they did not!  It turned out the 
only way to get constructors for objects in shared libraries to run was to 
stick a related object into the local image.  Hence, the only reason 
digest worked was because it included ost::MD5digest as a local static 
object in tests/digest.cpp.  In fact, if you create a static instance of 
any valid ost::xxx object in url1.cpp, then suddenly the constructors for 
all libccgnu2.dylib ohects also run, and everything is fine.  If you 
don't, then the static objects inside libccgnu2.dylib also never 
initialize...very wierd.

On Wed, 16 Apr 2003, Jon Wilson wrote:

> Many thanks for your help. I will look into a work around. Just seemed 
> odd that the same compiler should work Ok on one platform, but not another.
> Jon Wilson
> 
> David Sugar wrote:
> 
> > 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
> >>  
> >>
> >
> >
> >
> >
> > _______________________________________________
> > Bug-commoncpp mailing list
> > address@hidden
> > http://mail.gnu.org/mailman/listinfo/bug-commoncpp
> >
> 
> 
> 
> 
> _______________________________________________
> 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]