0

I'm using a TCPServer inside an activity. For simplify my design I make my activity ihnerit from TCPServerConnectionFactory. With that, my activity implement CreateConnection() method.

To create my TCPServer I make :

Poco::Net::TCPServer(this, serverSocket);

Unfortunately, when my TCPServer is destroyed, it also destroy (this) that I have given in parameter. So it call dtor of my activity, that's really annoying.

Looking deeply in the Poco code and I see that TCPServer create a Poco::shared_Ptr of (this) and when this Shared_Ptr is destroyed, it delete the content of the shared_ptr.

How I can pass (this) to TCPServer without be destroyed at the end of the life of TCPServer ?

lgm42
  • 591
  • 1
  • 6
  • 29

1 Answers1

0

You have to pass the connection factory allocated on the heap to the TCPServer. For an example, see TimeServer example:

class TimeServerConnection: public TCPServerConnection
{
  //...
};

// ...

class TimeServerConnectionFactory: public TCPServerConnectionFactory
{
  // ...
  TCPServerConnection* createConnection(const StreamSocket& socket)
  {
    return new TimeServerConnection(socket, _format);
  }
  // ...
};

// set-up a server socket
ServerSocket svs(port);
// retain the handle to shared ptr for later use
TCPServerConnectionFactory::Ptr pTSCF = new TimeServerConnectionFactory(format);
// set-up a TCPServer instance
TCPServer srv(pTSCF, svs);
Alex
  • 5,159
  • 4
  • 25
  • 33
  • Yes I know that I can do that with a new TSC() but I would like to give a persistant object which will not be destroyed automatically at the end of the life of the TCPServer – lgm42 Oct 21 '14 at 11:09
  • 1
    You can keep the shared pointer so it will not be destroyed in TCPServer because you will have the last ref count. I have updated the code in the answer above. – Alex Oct 21 '14 at 14:31
  • Ok I can do that but is there a possibility to create a shared ptr of this ? – lgm42 Nov 06 '14 at 13:20
  • TCPServerConnectionFactory::Ptr is Poco::SharedPtr, see https://github.com/pocoproject/poco/blob/develop/Net/include/Poco/Net/TCPServerConnectionFactory.h#L48 – Alex Nov 07 '14 at 01:25
  • Yes I know but if I would to use first idea I would to provide this to TCP server as this is TCPServerConnectionFactory. If think I can't do Poco::Net::TCPServer(TCPServerConnectionFactory::Ptr(this), serverSocket); – lgm42 Nov 07 '14 at 08:05
  • You can't do that because a new reference count with value of 1 will be created for the temporary SharedPtr and when temporary is destructed, this will be deleted. Poco::AutoPtr provides a solution to that, but it does not apply here. I could be missing something, but I'm not quite sure whether inheriting from the factory indeed simplifies the design. – Alex Nov 08 '14 at 03:02
  • It prevent me to create two different class and provide a lot of object from the server to the factory – lgm42 Nov 12 '14 at 08:25