Skip to content

Commit

Permalink
Make sure to actually copy the frame during the copy constructor. (#2)
Browse files Browse the repository at this point in the history
Otherwise we get bogus data in the destination sometimes.

Signed-off-by: Chris Lalancette <clalancette@openrobotics.org>
  • Loading branch information
clalancette authored and Gonzalo de Pedro committed Dec 20, 2021
1 parent 56d73b4 commit ac3baa6
Showing 1 changed file with 25 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,32 +97,33 @@ class ROSCvMatContainer
ROSCvMatContainer() = default;

IMAGE_TOOLS_PUBLIC
explicit ROSCvMatContainer(const ROSCvMatContainer &other) :
header_(other.header_), frame_(other.frame_), is_bigendian_(other.is_bigendian_)
{
if(std::holds_alternative<std::shared_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
explicit ROSCvMatContainer(const ROSCvMatContainer & other)
: header_(other.header_), frame_(other.frame_.clone()), is_bigendian_(other.is_bigendian_)
{
if (std::holds_alternative<std::shared_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
storage_ = std::get<std::shared_ptr<sensor_msgs::msg::Image>>(other.storage_);
} else if (std::holds_alternative<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
storage_ = std::make_unique<sensor_msgs::msg::Image>(
*std::get<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_));
}
}

IMAGE_TOOLS_PUBLIC
ROSCvMatContainer & operator=(const ROSCvMatContainer & other)
{
if (this != &other) {
header_ = other.header_;
frame_ = other.frame_.clone();
is_bigendian_ = other.is_bigendian_;
if (std::holds_alternative<std::shared_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
storage_ = std::get<std::shared_ptr<sensor_msgs::msg::Image>>(other.storage_);
} else if (std::holds_alternative<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
storage_ = std::make_unique<sensor_msgs::msg::Image>(
*std::get<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_));
}
else if(std::holds_alternative<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
storage_ = std::make_unique<sensor_msgs::msg::Image>( *std::get<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_) );
}

};

ROSCvMatContainer& operator=(const ROSCvMatContainer& other){
if (this != &other) {
header_ = other.header_;
frame_ = other.frame_;
is_bigendian_ = other.is_bigendian_;
if(std::holds_alternative<std::shared_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
storage_ = std::get<std::shared_ptr<sensor_msgs::msg::Image>>(other.storage_);
}
else if(std::holds_alternative<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_)) {
storage_ = std::make_unique<sensor_msgs::msg::Image>( *std::get<std::unique_ptr<sensor_msgs::msg::Image>>(other.storage_) );
}
}
return *this;
}
}
return *this;
}

/// Store an owning pointer to a sensor_msg::msg::Image, and create a cv::Mat that references it.
IMAGE_TOOLS_PUBLIC
Expand Down

0 comments on commit ac3baa6

Please sign in to comment.