How to avoid deep copy with std::vector of boost::shared_ptr

Viewed 64

I have written a simple template that takes as input a rosbag and stores message pointers in a std::vector using the cpp api.

template <class M>
class LocalizedMessageCollector
{
  void processBag(const rosbag::Bag& bag, const std::vector<std::string>& topics)
  {
    // create bag view
    rosbag::View view(bag, rosbag::TopicQuery(topics));
    
    BOOST_FOREACH (rosbag::MessageInstance const m, view)
    {
       ...

       boost::shared_ptr<M const> msg = m.instantiate<M>();
       msgCallback(msg);
       
       ...
  
    }
  }

  void msgCallback(const boost::shared_ptr<M const>& msg)
  {
   
    ...

    dataset_.push_back(std::make_pair(pose, msg));
  }


  std::vector<std::pair<tf2::Transform, boost::shared_ptr<M const>>> dataset_;
}

Unfortunately this code consumes an incredible amount of memory. I suspect that the push_back call is actually performing a deep copy, because:

  • as it is, the code consumes my 32GB of RAM after few seconds
  • if I comment the push_back line, I don't have this problem

My intention was to avoid deep copy and just store in the vector pointers to the messages. Can please someone point out what I'm doing wrong and explain me how to do properly what I'd like to do?

Thanks.

0 Answers
Related