Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct seg fault #48

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion plugins/datastreamer_plugin/fastdds/ReaderHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ReaderHandler::ReaderHandler(
, type_(type)
, listener_(listener)
, stop_(false)
, mtx_data_available_()
{
// Create data so it is not required to create it each time and avoid reallocation if possible
data_ = eprosima::fastrtps::types::DynamicDataFactory::get_instance()->create_data(type_);
Expand Down Expand Up @@ -108,6 +109,8 @@ void ReaderHandler::stop()
void ReaderHandler::on_data_available(
eprosima::fastdds::dds::DataReader* reader)
{
std::lock_guard<std::mutex> lock(mtx_data_available_);

eprosima::fastdds::dds::SampleInfo info;
eprosima::fastrtps::types::ReturnCode_t read_ret =
eprosima::fastrtps::types::ReturnCode_t::RETCODE_OK;
Expand All @@ -116,7 +119,7 @@ void ReaderHandler::on_data_available(
while (!stop_ && read_ret == eprosima::fastrtps::types::ReturnCode_t::RETCODE_OK)
{
// Read next data
read_ret = reader->take_next_sample(data_.get(), &info);
read_ret = reader->take_next_sample(data_, &info);

// If data has been read
if (read_ret == eprosima::fastrtps::types::ReturnCode_t::RETCODE_OK &&
Expand All @@ -130,6 +133,7 @@ void ReaderHandler::on_data_available(
numeric_data_info_,
data_,
numeric_data_);

utils::get_introspection_string_data(
string_data_info_,
data_,
Expand Down
5 changes: 4 additions & 1 deletion plugins/datastreamer_plugin/fastdds/ReaderHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#define _EPROSIMA_PLOTJUGGLERFASTDDSPLUGIN_PLUGINS_DATASTREAMERPLUGIN_FASTDDS_READERHANDLER_HPP_

#include <atomic>
#include <mutex>

#include <fastdds/dds/topic/Topic.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
Expand Down Expand Up @@ -128,7 +129,7 @@ struct ReaderHandler : public eprosima::fastdds::dds::DataReaderListener
eprosima::fastrtps::types::DynamicType_ptr type_;

//! Data Type element
eprosima::fastrtps::types::DynamicData_ptr data_;
eprosima::fastrtps::types::DynamicData* data_;

std::atomic<bool> stop_;

Expand All @@ -138,6 +139,8 @@ struct ReaderHandler : public eprosima::fastdds::dds::DataReaderListener
utils::TypeIntrospectionNumericStruct numeric_data_;
utils::TypeIntrospectionStringStruct string_data_;

std::mutex mtx_data_available_;

};

} /* namespace fastdds */
Expand Down
26 changes: 14 additions & 12 deletions plugins/datastreamer_plugin/utils/dynamic_types_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ void get_introspection_type_names(
const std::vector<TypeKind>& current_kinds_tree /* = {} */,
const std::string& separator /* = "/" */)
{
DEBUG("Getting types for type member " << base_type_name);

// Get type kind and store it as kind tree
TypeKind kind = type->get_kind();

DEBUG("Getting types for type member " << base_type_name << " of kind " + std::to_string(kind));

switch (kind)
{
case fastrtps::types::TK_BOOLEAN:
Expand Down Expand Up @@ -180,7 +180,7 @@ void get_introspection_type_names(

void get_introspection_numeric_data(
const TypeIntrospectionCollection& numeric_type_names,
const DynamicData_ptr& data,
DynamicData* data,
TypeIntrospectionNumericStruct& numeric_data_result)
{
DEBUG("Getting numeric data");
Expand All @@ -206,7 +206,7 @@ void get_introspection_numeric_data(
std::get<TypeKind>(member_type_info);

// Get Data parent that has the member we are looking for
const auto& parent_data = get_parent_data_of_member(
auto parent_data = get_parent_data_of_member(
data,
members,
kinds);
Expand All @@ -220,7 +220,7 @@ void get_introspection_numeric_data(

void get_introspection_string_data(
const TypeIntrospectionCollection& string_type_names,
const eprosima::fastrtps::types::DynamicData_ptr& data,
DynamicData* data,
TypeIntrospectionStringStruct& string_data_result)
{
DEBUG("Getting string data");
Expand All @@ -246,7 +246,7 @@ void get_introspection_string_data(
std::get<TypeKind>(member_type_info);

// Get Data parent that has the member we are looking for
const auto& parent_data = get_parent_data_of_member(
auto parent_data = get_parent_data_of_member(
data,
members,
kinds);
Expand All @@ -258,13 +258,15 @@ void get_introspection_string_data(
}
}

DynamicData_ptr get_parent_data_of_member(
const DynamicData_ptr& data,
DynamicData* get_parent_data_of_member(
DynamicData* data,
const std::vector<MemberId>& members_tree,
const std::vector<TypeKind>& kind_tree,
unsigned int array_indexes /* = 0 */)
{

DEBUG("Getting parent data of type " << std::to_string(kind_tree[array_indexes]));

if (array_indexes == members_tree.size() - 1)
{
// One-to-last value, so this one is the value to take, as it is the parent of the data
Expand All @@ -285,7 +287,7 @@ DynamicData_ptr get_parent_data_of_member(
data->get_complex_value(&child_data, member_id);

return get_parent_data_of_member(
DynamicData_ptr(child_data),
child_data,
members_tree,
kind_tree,
array_indexes + 1);
Expand All @@ -306,11 +308,11 @@ DynamicData_ptr get_parent_data_of_member(
}

double get_numeric_type_from_data(
const DynamicData_ptr& data,
DynamicData* data,
const MemberId& member,
const TypeKind& kind)
{
DEBUG("Getting numeric data of kind " << kind << " in member " << member);
DEBUG("Getting numeric data of kind " << std::to_string(kind) << " in member " << member);

switch (kind)
{
Expand Down Expand Up @@ -354,7 +356,7 @@ double get_numeric_type_from_data(
}

std::string get_string_type_from_data(
const DynamicData_ptr& data,
DynamicData* data,
const MemberId& member,
const TypeKind& kind)
{
Expand Down
12 changes: 6 additions & 6 deletions plugins/datastreamer_plugin/utils/dynamic_types_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ void get_introspection_type_names(
*/
void get_introspection_numeric_data(
const TypeIntrospectionCollection& numeric_type_names,
const DynamicData_ptr& data,
DynamicData* data,
TypeIntrospectionNumericStruct& numeric_data_result);

/**
* @brief TODO
*/
void get_introspection_string_data(
const TypeIntrospectionCollection& string_type_names,
const DynamicData_ptr& data,
DynamicData* data,
TypeIntrospectionStringStruct& string_data_result);

/**
Expand All @@ -134,8 +134,8 @@ void get_introspection_string_data(
* @param array_indexes [in]
* @return DynamicData_ptr
*/
DynamicData_ptr get_parent_data_of_member(
const DynamicData_ptr& data,
DynamicData* get_parent_data_of_member(
DynamicData* data,
const std::vector<MemberId>& members,
const std::vector<TypeKind>& kinds,
unsigned int array_index = 0);
Expand All @@ -149,7 +149,7 @@ DynamicData_ptr get_parent_data_of_member(
* @return numeric data of the member casted to double
*/
double get_numeric_type_from_data(
const DynamicData_ptr& data,
DynamicData* data,
const MemberId& member,
const TypeKind& kind);

Expand All @@ -162,7 +162,7 @@ double get_numeric_type_from_data(
* @return string data of the member casted to string
*/
std::string get_string_type_from_data(
const DynamicData_ptr& data,
DynamicData* data,
const MemberId& member,
const TypeKind& kind);

Expand Down