Menu

TCPSession segfaults / hangs

Help
2004-11-17
2012-11-20
  • Rami Saarinen

    Rami Saarinen - 2004-11-17

    I have a small project in which I use commoncpp2 mainly for threading and network stuff. In the project there is a TCPListener (a thread) that waits for a new connection. It creates a TCPSessionThread for each new connection. TCPSessionThread is basically a wrapper class around TCPSession and contains mainly stuff like a pointer to object for callback purposes.

    TCPListener looks like this (mostly copied from the examples):
    ...
      void run() {
        try {
          TCPSessionThread<T> *tcp;
          while(enabled) {
        while(soc->isPendingConnection()) {
          tcp = new TCPSessionThread<T>(*soc,this->master);
          tcp->detach();
        }
          }
        } catch(Socket *socket) {
    ...

    And TCPSessionThread is just as simple.. could do some work on the buffer & data handling :) :

    ...
    void run() {
        ssize_t val=1;
        char buf[512];
        string *ret=new string();   
        while(val>0) {
          val=readData((void *)buf,512);
          string s(buf,val);
          ret->append(s);
          if(ret->size()>MAX_IN_BYTECOUNT)
        throw OwnException("Buffer overrun in TCPSessionThread");
        }
        ((T)this->master)->handleMessage(ret);
      }

      void final() {
        delete this;
      }

    ...

    The program works just fine on my desktop computer that has Fedora core 2 (and gcc 3.3.3) and commoncpp2 1.0.9 installed.

    It also works fine on windows 2000 (with ccpp2 1.0.13 IIRC).

    And on one old laptop with FC2 and ccpp2 1.1.0 ecept that now it prints Free() Invalid pointer error messages quite a lot.

    But on another laptop where I need to get it to work  it does not work at all. The setup is Fedora core 3 (gcc 3.4.2) and any version on ccpp2 (from 1.0.13 to 1.2.6).

    The problem seems to be TCPSession and especially the final() method of it.

    1. If I use older version of ccpp2, I get free() invalid pointer error from glibc and the program aborts. If I comment out the delete this line from final method. The program seg-falults (IIRC)

    2. If I use ccpp2 1.2.6 I get the same free() invalid pointer error, but if I comment out the "delete this" from the final method the TCPListener seems to hang for minutes after the first TCPSession is created. After several minutes the program continues and finally handles the pending connections. 

    I do use mutexes quite a lot, but they should not be the cause of the lock-up/stall as the program works fine on other systems. 

    Any ideas? I am about to remove the FC3 installation and install FC2 instead as I really need to get it working on the laptop, but it is a pain to reinstall all the needes stuff again. So I'd be very happy if someone can tell me how to bypass this problem.

    Thanks!

    --
    Rami Saarinen

     
    • David Sugar

      David Sugar - 2004-12-22

      In later versions of the library, we manage detached thread deletion rather than have people put a delete this in final.

      As to the long delay, I am not sure on that....I will have to look at that issue.

       
    • Dominik Deobald

      Dominik Deobald - 2005-03-28

      I seem to have a similar problem. My code is also very simple...

      class myTCPSocket : public TCPSocket {
      protected:
          bool onAccept(const InetHostAddress &ia, tpport_t port);

      public:
          myTCPSocket(InetAddress &ia, short &nPort);
      };

      class myTCPSession : public TCPSession {
      private:
          void run(void);
          void final(void);

      public:
          myTCPSession(TCPSocket &server);
      };

      myTCPSocket::myTCPSocket(InetAddress &ia, short &nPort) : TCPSocket(ia, nPort) {};
      myTCPSession::myTCPSession(TCPSocket &server) : TCPSession(server) {}

      bool myTCPSocket::onAccept(const InetHostAddress &ia, tpport_t port) {
          cout << "accepting from: " << ia << ":" << port << endl;
          return true;
      }

      void myTCPSession::run(void) {
          tpport_t port;
          IPV4Address addr = getLocal(&port);
          *tcp() << "It's alive!" << endl;
      }

      void myTCPSession::final(void) {
          cout << "Final" << endl;
      }

      int main() {
          myTCPSession *tcp;

          InetAddress addr;
          addr = "192.168.55.50";
          short nPort = 80;

          myTCPSocket server(addr, nPort);

          while(alive && server.isPendingConnection(20000)) {
              tcp = new myTCPSession(server);
              tcp->detach();
          }

          return 0;
      }

      I use MS Visual C++ .net 2.1 under Windows XP.

      I get an access violation after the first thread has finished (i.e. I get "Final", then it's over). However the Example (tcpthread.cpp) which comes along with CommonC++ works fine, so I guess, it's just a compiler / linker setting. Is there anything I have to make sure?

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.