Skip to content

Commit

Permalink
Fixed segfault issue by adding virtual destructor
Browse files Browse the repository at this point in the history
  • Loading branch information
SamFlt committed Nov 15, 2024
1 parent a9caed8 commit 8b4ed72
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 37 deletions.
46 changes: 11 additions & 35 deletions modules/tracker/rbt/include/visp3/rbt/vpDynamicFactory.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,14 @@ class VISP_EXPORT vpDynamicFactory
m_jsonBuilders[key] = function;
}

void registerTypeRaw(const std::string &key, const std::function<std::shared_ptr<T>(const std::string &)> &function)
void registerTypeRaw(const std::string &key, const std::function<std::shared_ptr<T>(const std::string &)> function)
{
if (m_jsonBuilders.find(key) != m_jsonBuilders.end() || m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
throw vpException(vpException::badValue, "Type %s was already registered in the factory", key.c_str());
}
std::cout << "IN REGISTERING RAW TYPE" << std::endl;
std::cout << "Key is " << key << std::endl;

// m_jsonRawBuilders.insert({ key, function });
m_jsonRawBuilders.insert({ key, function });
// m_jsonRawBuilders[key] = [](const std::string &s) -> std::shared_ptr<T> {
m_raw = [](const std::string &s) -> std::shared_ptr<T> {
std::cout << "IN CPP LAMBDA, before calling python fn" << std::endl;
return nullptr;
// return function(s);
};
std::cout << "in register: THIS is " << (void *)(this) << std::endl;

std::cout << "IN REGISTERING RAW TYPE END" << std::endl;

}

Expand All @@ -87,44 +77,30 @@ class VISP_EXPORT vpDynamicFactory
if (m_jsonBuilders.find(key) != m_jsonBuilders.end()) {
return m_jsonBuilders[key](j);
}

else if (m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
return m_jsonRawBuilders[key](j.dump());
}
else {
std::cout << "in call: THIS is " << (void *)(this) << std::endl;

std::cout << "CALLING RAW METHOD!" << std::endl;
std::string rawRep = j.dump();
std::cout << rawRep << std::endl;
std::cout << (void *)(&m_raw) << std::endl;
std::shared_ptr<T> res = m_raw(rawRep);
std::cout << "After RAW METHOD!" << std::endl;
return res;
return nullptr;
}

// else if (m_jsonRawBuilders.find(key) != m_jsonRawBuilders.end()) {
// std::cout << "Before accessing map" << std::endl;
// return m_jsonRawBuilders[key](j.dump());
// }
// else {
// return nullptr;
// }
}


void setJsonKeyFinder(const std::function<std::string(const nlohmann::json &)> &finderFn)
{
m_keyFinder = finderFn;
}
#endif

protected:
virtual ~vpDynamicFactory() { }

vpDynamicFactory()
{
std::cout << "in constructor: THIS is " << (void *)(this) << std::endl;
}
protected:

vpDynamicFactory() = default;
#if defined(VISP_HAVE_NLOHMANN_JSON)
std::map<std::string, std::function<std::shared_ptr<T>(const nlohmann::json &)>> m_jsonBuilders;
std::map<std::string, std::function<std::shared_ptr<T>(const std::string &)>> m_jsonRawBuilders;
std::function<std::shared_ptr<T>(const std::string &)> m_raw;

std::function<std::string(const nlohmann::json &)> m_keyFinder; //! Function to retrieve the key from a json object
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ class VISP_EXPORT vpRBFeatureTrackerFactory : public vpDynamicFactory<vpRBFeatur
static vpRBFeatureTrackerFactory &getFactory()
{
static vpRBFeatureTrackerFactory factory;
std::cout << "RBFEATUREFACTORY = " << (void *)(&factory) << std::endl;
return factory;
}

};

END_VISP_NAMESPACE
Expand Down
2 changes: 2 additions & 0 deletions modules/tracker/rbt/include/visp3/rbt/vpRBTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ class VISP_EXPORT vpRBTracker
std::string getModelPath() const { return m_modelPath; }
void setModelPath(const std::string &path);

std::vector<std::shared_ptr<vpRBFeatureTracker>> getFeatureTrackers() { return m_trackers; }

vpCameraParameters getCameraParameters() const;
void setCameraParameters(const vpCameraParameters &cam, unsigned h, unsigned w);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ BEGIN_VISP_NAMESPACE

vpRBFeatureTrackerFactory::vpRBFeatureTrackerFactory()
{
std::cout << "in RBFEATURE constructor: THIS is " << (void *)(this) << std::endl;
setJsonKeyFinder([](const nlohmann::json &j) -> std::string {
return j.at("type");
});
Expand Down

0 comments on commit 8b4ed72

Please sign in to comment.