Having three classes
class A {
public:
virtual const std::string Foo1 () const = 0;
virtual void Foo2 (int direction, std::shared_ptr<A> aPtr) = 0;
A(const std::string & name): objName(name) {}
vitrual ~A() {}
private:
const std::string objName;
std::unordered_map<int, std::shared_ptr<A>> neighbours;
}
class B: public A {
public:
void Foo2 (int direction, std::shared_ptr<A> aPtr) override {
if (direction > 3){
throw std::invalid_argument("...");
}
this->neighbours.insert(std::make_pair(direction, aPtr));
}
}
class C: public B {
public:
const std::string Foo1 () const override {
return desc;
}
private:
const static std::string desc; // Value has been set in cpp file
}
running this code in a test below
C c("test");
CPPUNIT_ASSERT_THROW(c.Foo2(0, std::make_shared<B>()), std::invalid_argument);
causing free(): invalid pointer: 0x00000000017ccc88 *** error. When I run the code in valgrind it shows that the cause of error is in the A constructor: with this message: 88 bytes in 1 blocks are definitely lost in loss record 1 of 6
What is the problem? Can I have not const member variables with shared_ptr or am I passing my shared_ptr wrong?
Aucun commentaire:
Enregistrer un commentaire