views:

195

answers:

1

I'm trying to add a boost::ptr_vector to a std::deque, using push_back(). When I do, I get a BOOST::ASSERT for the typeid mismatch.

In "boost_ptr_container_clone_allocator"

     T* res = new T( r );
     BOOST_ASSERT( typeid(r) == typeid(*res) &&
                   "Default new_clone() sliced object!" );
     return res;

From TotalView, res and r:

Function "boost::new_clone<diagnostic_database_loader::DiagnosticDBClass>":
r:                                    (diagnostic_database_loader::DiagnosticDBClass  const &)
Local variables:
res:                                  0x082534f8 -> (diagnostic_database_loader::DiagnosticDBClass)

They look the same to me.

The ptr_vector I'm trying to add has instances of diagnostic_database_loader::JointDiagnosticDBClass, which is derived from the diagnostic_database_loader::DiagnosticDBClass above.

I printed out the typeid of the elements in the ptr_vector

boost::ptr_vector<DiagnosticDBClass> items(loader->getData());
>>> N26diagnostic_database_loader22JointDiagnosticDBClassE

I tried to reproduce this with a simple test program, but I'm not having the same problem.

#include "iostream"
#include <boost/ptr_container/ptr_vector.hpp>
#include <deque>

class Item
{
public:
  int my_val;
  Item() : my_val(1) { }

  int getMyVal() { return my_val; }
};
class SmallItem : public Item
{
public:
  SmallItem() : Item() { my_val = 2; }
};

class TinyItem : public SmallItem
{
public:
  TinyItem() : SmallItem() { my_val = 3; }
};

class MyClass
{
private:
  boost::ptr_vector<SmallItem> items_;  

public:
  MyClass() 
  { 
    for (int i = 0; i < 10; ++i)
    {
      SmallItem *it = new TinyItem();
      items_.push_back(it);
     }
  }

 std::auto_ptr<boost::ptr_vector<SmallItem> > getData() { return items_.release(); }
 };

std::deque<boost::ptr_vector<SmallItem> > Buffer;

int totalItems(boost::ptr_vector<SmallItem> &items)
{
  int total = 0;
  boost::ptr_vector<SmallItem>::iterator it;
  for (it = items.begin(); it != items.end(); ++it)
    total += (*it).getMyVal();
  return total;
}

int main(int argc, char **argv)
{
  MyClass cls;

  boost::ptr_vector<SmallItem> items(cls.getData());

  std::cout << "SmallItem typeid   " << typeid(items[0]).name() << std::endl;

  fprintf(stdout, "I found %d total!\n", totalItems(items));

  Buffer.push_back(items);

  fprintf(stdout, "I pushed back into the deque!\n");

   boost::ptr_vector<SmallItem> items2 = Buffer.front();
   Buffer.pop_front();
   fprintf(stdout, "I still found %d total in the new vector!\n", totalItems(items2));

  items2.release();
  fprintf(stdout, "I found %d total after I released!\n", totalItems(items2));

  return 0;
}

The test program works fine.

Does anyone know how to
* Reproduce the problem in the test code?
* Fix the problem in the real program?

If anyone wants the full code:
https://code.ros.org/svn/wg-ros-pkg/trunk/sandbox/diagnostic_database_loader

+1  A: 

You should specialize the new_clone and delete_clone functions as described in the documentation.

Alternatively you could specify your own clone allocator as the second argument of ptr_vector:

class Item
{
public:
  int my_val;
  Item() : my_val(1) { }

  Item* clone() const
  {
    Item* item = do_clone();
    BOOST_ASSERT(typeid(*this) == typeid(*item) &&
                 "do_clone() sliced object!");
    return item;
  }

  int getMyVal() { return my_val; }

private:
  // new virtual member function, overload in all derived classes.
  virtual Item* do_clone() const
  {
    return new Item(*this);
  }
};

class SmallItem : public Item
{
public:
  SmallItem() : Item() { my_val = 2; }

private:
  virtual Item* do_clone() const
  {
    return new SmallItem(*this);
  }
};

struct ItemCloner
{
  static Item* allocate_clone(const Item& item)
  {
    return item.clone();
  }

  static void deallocate_clone(const Item* item)
  {
    delete item;
  }
};

int main()
{
   boost::ptr_vector<Item, ItemCloner> items;
   // and so on...
}
dalle
Thanks, I tried out the "ItemCloner" method, and it worked in my program. Which implementation is preferred? It seems like specifying the Cloner in every vector is a little cumbersome.With these cloners, does this mean we're making a copy for every array release?
kevbo
@kevbo: Each time a `ptr_vector` is copied its internal data is also copied. If you don't want that behavior you should probably be using `vector< shared_ptr<Item> >` instead.
dalle