Repository navigation
Closed
Labels
Description
Bug report
Required Info:
- Operating System:
- Ubuntu 22.04
- Installation type:
- source install
- DDS implementation:
- test on Fast-RTPS, CycloneDDS, iceoryx DDS
- Client library (if applicable):
- rclcpp
Steps to reproduce issue
TestMsg.msg:
# uint32[1048576] data # work fine
uint32[2097152] data # fail
uint8[128] name
uint8 name_sizemyString.hpp:
#ifndef MY_STRING_HPP
#define MY_STRING_HPP
#include "rclcpp/rclcpp.hpp"
#include "tutorial_interfaces/msg/test_msg.hpp"
template <>
struct rclcpp::TypeAdapter<
std::string,
tutorial_interfaces::msg::TestMsg>
{
using is_specialized = std::true_type;
using custom_type = std::string;
using ros_message_type = tutorial_interfaces::msg::TestMsg;
static void
convert_to_ros_message(
const custom_type &source,
ros_message_type &destination)
{
std::memcpy(destination.name.data(), source.data(), source.size());
destination.name_size = source.size();
}
static void
convert_to_custom(
const ros_message_type &source,
custom_type &destination)
{
destination.resize(source.name_size);
std::memcpy(destination.data(), source.name.data(), source.name_size);
}
};
RCLCPP_USING_CUSTOM_TYPE_AS_ROS_MESSAGE_TYPE(std::string, tutorial_interfaces::msg::TestMsg);
#endif // !MY_STRING_HPPHere, std::string is a custom type.
pub.cpp:
#include <chrono>
#include <memory>
#include <string>
#include "rclcpp/rclcpp.hpp"
#include "myString.hpp"
using namespace std::chrono_literals;
using TopicPub = std::string;
class MinimalPublisher : public rclcpp::Node
{
public:
MinimalPublisher()
: Node("minimal_publisher"), count_(0)
{
auto timer_callback =
[this]() -> void
{
std::string name = "myName";
this->publisher_->publish(name);
};
publisher_ = this->create_publisher<TopicPub>("topic", 10);
timer_ = create_wall_timer(30ms, timer_callback);
}
private:
rclcpp::TimerBase::SharedPtr timer_;
rclcpp::Publisher<TopicPub>::SharedPtr publisher_;
size_t count_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalPublisher>());
rclcpp::shutdown();
return 0;
}sub.cpp:
#include <memory>
#include "rclcpp/rclcpp.hpp"
#include <chrono>
#include "myString.hpp"
using TopicSub = std::string;
class MinimalSubscriber : public rclcpp::Node
{
public:
MinimalSubscriber()
: Node("minimal_subscriber")
{
auto callback = [this](const TopicSub &msg)
{
std::cout << "msg = " << msg << std::endl;
};
subscription_ = this->create_subscription<TopicSub>("topic", 10, callback);
}
private:
rclcpp::Subscription<TopicSub>::SharedPtr subscription_;
};
int main(int argc, char *argv[])
{
rclcpp::init(argc, argv);
rclcpp::spin(std::make_shared<MinimalSubscriber>());
rclcpp::shutdown();
return 0;
}Expected behavior
Message sent successfully in the publisher and received in the subscriber.
Actual behavior
[ros2run]: Segmentation fault in the publisher.
Additional information
In TestMsg.msg, I define two different data size messages (1MB and 2MB). It works well with 1MB but fails with 2MB.
Activity
Zard-C commentedon Jan 28, 2024
Hi, imo
allbluelai commentedon Jan 29, 2024
Hello, @Zard-C
Apologies for not mentioning the version. I run the code in ROS2 Humble.
It crashes when it calls publish().
some gdb info:
Segmentation fault occurs with Type Adapter when using large data siz…
fujitatomoya commentedon Jan 30, 2024
this can be reproducible with mainline rolling branch. as reported, with 1MB data, this works okay but 2MB.
it seems to be okay before calling
rclcpp::Publisher, right after the function call, memory is corrupted?roncapat commentedon Feb 6, 2024
It may be curious to understand where the message gets created by rclcpp during conversion (stack or heap) and if the issue happens on different machines or architectures at the same size boundary. Another thing is: has anyone tried to do a dycotomic research between 1Mb & 2Mb to find the exact spot things start to break?
thomasmoore16 commentedon Feb 7, 2024
This is likely being caused because the
ROSMessageTypeis being allocated on the stack in thePublisher::publish()methods:rclcpp/rclcpp/include/rclcpp/publisher.hpp
Lines 324 to 326 in c500695
rclcpp/rclcpp/include/rclcpp/publisher.hpp
Lines 374 to 377 in c500695
The safe thing would be to allocate the message on the heap:
However, it would probably be good to have a similar TODO as in
AnySubscriptionCallback::dispatch()to allow small messages to be allocated on the stack:rclcpp/rclcpp/include/rclcpp/any_subscription_callback.hpp
Lines 535 to 539 in c500695
Even better would be for the option to create on the stack or heap to be configurable.
roncapat commentedon Feb 7, 2024
Spotted then ;) It is something that needs to be avoided IMHO.
audrow commentedon Feb 15, 2024
Perhaps this could be fixed by using a shared or unique pointer, like is done in other locations?
rclcpp/rclcpp/include/rclcpp/publisher.hpp
Lines 333 to 339 in 091f29f