Why should I provide a pointer and not SharedPtr to some poco methods
We are using Poco in our project and we found 3 cases where we are confused by poco and his pointing gesture.
Most of the time, when you call the mathod of the poco class, it takes a Poco :: SharedPtr <> parameter, but sometimes it takes a pointer in the parameter. After taking ownership of the pointer creating SharedPtr <> inside its class.
Someday, we would like to provide a class to retain ownership of it. For example, to prevent it from coming off at the end of each call.
For example, this class uses the poco :: TaskManager class to start a task. But we have to be very careful about this, because the object we created is owned by 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 named p in the local context to expose it to the HTTPServer. but we have to create it on every call! If we would rather do Poco :: ShaaredPtr on a member, just return it. But if we do it with this pointer, we won't be able to know if this pointer is alive.
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;
}
}
another case refers to the already message TCPServerConnectionFactory on Stack Overflow: Unable to use Poco TCPServer and TCPServerConnectionFactory
Why are some methods always proprietary? Is it not possible to have a signature to provide SharedPtr <> instead? I think there are not many changes in poco lib.
Any explanation?
source to share