Skip to content

Commit

Permalink
service: fixup shared_from_this saga for setOwner/setParent.
Browse files Browse the repository at this point in the history
We now catch the bad weak ptr and create the shared_ptr
ourselves. This is allowed since we know that it is stored
in the other Service and not destructed at the end of the
function.

Signed-off-by: Ruben Smits <ruben.smits@intermodalics.eu>
  • Loading branch information
Ruben Smits committed Mar 7, 2014
1 parent df8b7a9 commit d46e4be
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
28 changes: 19 additions & 9 deletions rtt/Service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,7 @@ namespace RTT {

Service::Service(const std::string& name, TaskContext* owner)
: mname(name),
#if BOOST_VERSION >= 104000 && BOOST_VERSION <= 105030
mowner(owner),
#else
mowner(0),
#endif
parent()
{
// Inform DataFlowInterface.
Expand Down Expand Up @@ -117,9 +113,8 @@ namespace RTT {
try {
return shared_from_this();
} catch( boost::bad_weak_ptr& /*bw*/ ) {
log(Error) <<"When using boost < 1.40.0 : You are not allowed to call provides() on a Service that does not yet belong to a TaskContext or another Service." << endlog();
log(Error) <<"When using boost < 1.40.0 or boost > 1.53.0 : You are not allowed to call provides() on a Service that does not yet belong to a TaskContext or another Service (for example in a constructor)." << endlog();
log(Error) <<"Try to avoid using provides() in this case: omit it or use the service directly." <<endlog();
log(Error) <<"OR: upgrade to boost 1.40.0, then this error will go away." <<endlog();
throw std::runtime_error("Illegal use of provides()");
}
}
Expand All @@ -132,7 +127,14 @@ namespace RTT {
return sp;
sp = boost::make_shared<Service>(service_name, mowner);
sp->setOwner( mowner );
sp->setParent( shared_from_this() );
// we pass and store a shared ptr in setParent, so we hack it like this:
shared_ptr me;
try {
me = shared_from_this();
} catch ( boost::bad_weak_ptr& bw ) {
me.reset(this); // take ownership
}
sp->setParent( me );
services[service_name] = sp;
return sp;
}
Expand Down Expand Up @@ -259,8 +261,16 @@ namespace RTT {

for( Services::iterator it= services.begin(); it != services.end(); ++it) {
it->second->setOwner( new_owner );
if (new_owner)
it->second->setParent( shared_from_this() );
if (new_owner) {
// we pass and store a shared ptr in setParent, so we hack it like this:
shared_ptr me;
try {
me = shared_from_this();
} catch ( boost::bad_weak_ptr& bw ) {
me.reset(this); // take ownership
}
it->second->setParent( me );
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions rtt/Service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
#include <boost/type_traits/function_traits.hpp>
#include <boost/function_types/components.hpp>
#include <boost/enable_shared_from_this.hpp>
#if BOOST_VERSION >= 104000 && BOOST_VERSION <= 105030
#if BOOST_VERSION >= 104000 && BOOST_VERSION < 105300
#include <boost/smart_ptr/enable_shared_from_this2.hpp>
#endif

Expand All @@ -82,7 +82,7 @@ namespace RTT
: public OperationInterface,
public ConfigurationInterface,
public DataFlowInterface,
#if BOOST_VERSION >= 104000 && BOOST_VERSION <= 105030
#if BOOST_VERSION >= 104000 && BOOST_VERSION < 105300
public boost::enable_shared_from_this2<Service>
#else
public boost::enable_shared_from_this<Service>
Expand Down
2 changes: 2 additions & 0 deletions rtt/plugin/PluginLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -713,6 +713,8 @@ bool PluginLoader::loadInProcess(string file, string shortname, string kind, boo
try {
// Load into process (TaskContext* == 0):
success = (*loading_lib.loadPlugin)( 0 );
} catch(std::exception& e) {
log(Error) << "Loading "<< plugname <<" threw an exception: "<< e.what() << endlog();
} catch(...) {
log(Error) << "Unexpected exception in loadRTTPlugin !"<<endlog();
}
Expand Down

0 comments on commit d46e4be

Please sign in to comment.