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