Search code examples
c++pointerspoco-librariesconceptual

Why I must provide pointer and not SharedPtr into some methods of poco


We are using Poco in our project and we've found 3 cases where we are embarrassed with poco and its pointer gesture.

In most of cases when you call a mathod of a poco class, it take in parameter a Poco::SharedPtr<> but sometimes, it take a pointer in parameter. After it take ownership of the pointer creating a SharedPtr<> inside its class.

Sometime we would like to provide a class to poco keeping the ownership of it. For example to prevent it from being detroy at the end of each call.

For example, this class use the poco::TaskManager class to run a task. But we must be very careful about this because the ownership of the object we have created belongs to poco::TaskManager.

CMyClass()
{

  m_xplTask = new CXplServiceTask(...);

  //task manager take the ownership !! (why ??)
  m_taskManager.start(m_xplTask);
}

~CMyClass()
{
    //do not delete m_xplTask; because owned by Poco::TaskManager ;-(
}

Another example :

We use a CRestRequestHandler pointer named p in a locally context to provide it to the HTTPServer. but we must create it at each call ! If we could we prefer to make a Poco::ShaaredPtr in member an simply return it. But if we do that with this pointer we can't know if the pointer is alive or not.

Poco::Net::HTTPRequestHandler* CHttpRequestHandlerFactory::createRequestHandler(const Poco::Net::HTTPServerRequest& request)
{
  //do not keep pointers in shared_ptr or somewhere else, because poco take ownership ;-(

  if (boost::istarts_with(request.getURI(), m_webSocketKeyword))
     return new CWebSocketRequestHandler(m_notificationCenter);
  else if (boost::istarts_with(request.getURI(), m_restKeywordBase))
  {
     CRestRequestHandler * p = new CRestRequestHandler(m_restKeywordBase);

     //do some very long init
     std::vector< boost::shared_ptr<web::rest::service::IRestService> >::iterator i;
     for (i = m_restService.begin(); i != m_restService.end(); ++i)
        p->registerRestService(*i);
     p->initialize();

     return p;
  }
  else
  {
     CWebsiteRequestHandler * p = new CWebsiteRequestHandler(m_configDocRoot);
     std::map<std::string, std::string>::iterator i;
     for (i = m_alias.begin(); i != m_alias.end();++i)
        p->configureAlias(i->first, i->second);
     return p;
  }

}

the other case concern the TCPServerConnectionFactory already post in stack overflow: Can't use Poco TCPServer and TCPServerConnectionFactory

Why some methods always take ownership ? Is it not possible to have a signature to provide a SharedPtr<> instead ? I think there is not many modification into poco lib to do that.

Any explanation ?


Solution

  • See discussion on the POCO Forum.