Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is a pure virtual function being called

Tags:

c++

I'm using libPoco to create a dummy server to test some client code.

class ServerRunnable: public Poco::Runnable {
  public:
ServerRunnable(StreamSocket conn) : conn(conn) {
}

void run(){
  string mess("Can you hear me?\n");
  try{
    this->conn.sendBytes(mess.c_str(), mess.size());
  } catch (Poco::Exception& ex){
    cerr << ex.displayText() << endl;
    return;
  }
  cerr << "The message has been sent." << endl;
}

void setConn(StreamSocket inConn){
  this->conn = inConn;
}
  private:
StreamSocket conn;
};


int main(int argc, char **argv){
  ServerSocket s;
  try{
    s.bind(8083, true);
  } catch (Exception &ex){
    cerr << ex.displayText() << endl;
    exit(1);
  }
  s.listen(124);

  Poco::ThreadPool Pool(10, 25, 60, 128);
  while(1){
    try{
      StreamSocket conn = s.acceptConnection();
      ServerRunnable serveIt(conn);

      Pool.start(serveIt);
    }  catch (Exception &ex){
      cerr << ex.displayText() << endl;
      Pool.joinAll();
      exit(1);
    }
  } 
  return 0;
 }

Poco::Runnable is an abstract class, and I'm pretty sure that the run is a pure virtual function. Pool.start(serveIt) seems to be calling ServerRunnable's run. When I run it from the console I consistently get pure virtual method called error. However, if I'm in gdb stepping through the code then I'll successfully accept the connection from the client and send them the data. ServerRunnable's run is not a pure virtual function and that is what should be called.

Code examples for libPoco's Threading are at http://pocoproject.org/slides/130-Threads.pdf

I'm also thinking that I might be calling pure virtual method in the constructor, but there is nothing in the contructor and I'm just using the default destuctor. Is there someway to nail down the where and what pure virtual function is being called? In gdb? Thanks.

like image 381
CallMeNorm Avatar asked Jan 13 '23 01:01

CallMeNorm


1 Answers

The problem is most likely that you serverIt object goes out of scope before it's run method is called.

You don't have any control over when a thread may run, so it may be that the loop iterates before the run method of your class gets called, but then your object have been destroyed, and with its destruction of course the virtual function table.

like image 166
Some programmer dude Avatar answered Jan 24 '23 05:01

Some programmer dude