Skip to content

Commit

Permalink
Fix factory map leak (ros#131)
Browse files Browse the repository at this point in the history
Signed-off-by: y-okumura <[email protected]>
  • Loading branch information
y-okumura-isp committed Jan 20, 2021
1 parent a62623e commit de5c77a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion include/class_loader/meta_object.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class CLASS_LOADER_PUBLIC AbstractMetaObjectBase
* TEMPLATE SUBCLASSES, OTHERWISE THEY WILL PULL IN A REDUNDANT METAOBJECT
* DESTRUCTOR OUTSIDE OF libclass_loader WITHIN THE PLUGIN LIBRARY! T
*/
~AbstractMetaObjectBase();
virtual ~AbstractMetaObjectBase();

/**
* @brief Gets the literal name of the class.
Expand Down
22 changes: 20 additions & 2 deletions src/class_loader_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,26 @@ std::recursive_mutex & getPluginBaseToFactoryMapMapMutex()

BaseToFactoryMapMap & getGlobalPluginBaseToFactoryMapMap()
{
static BaseToFactoryMapMap instance;
return instance;

static std::shared_ptr<BaseToFactoryMapMap> instance;

// TODO: need lock?
// TODO: we may use unique_ptr but use shared_ptr for PoC
if(instance == nullptr) {
instance = std::shared_ptr<BaseToFactoryMapMap>(
new BaseToFactoryMapMap,
[](BaseToFactoryMapMap *p) mutable
{
for(auto it_map: *p) {
for(auto it: it_map.second) {
if(it.second) {
delete(it.second);
}
}
}
});
}
return *instance;
}

FactoryMap & getFactoryMapForBaseClass(const std::string & typeid_base_class_name)
Expand Down

0 comments on commit de5c77a

Please sign in to comment.