I have a simple class structure that uses boost::shared_ptr.

it looks something like -

Point class:

boost::shared_ptr<PointPrism> getPrismFromDirection(const Point3& direction) const
{
    return boost::shared_ptr<PointPrism>(new PointPrism(_position, direction));
}

boost::shared_ptr<PointPrism> getPrismFromAimingPoint(const Point3& aimingPoint)
{
    boost::shared_ptr<PointPrism> prism = getPrismFromDirection((aimingPoint - _position).normalized());
    prism->setAimingPoint(aimingPoint);
    return prism;
}    

Main class:

main()
{
    boost::shared_ptr<Point> p; //initialized somewhere in this code
    boost::shared_ptr<PointPrism> prism = p->getPrismFromAimingPoint(aimingPoint);

    //here it looks like the prism object is just fine
    boost::this_thread::sleep(boost::posix_time::seconds(10));
    //here both VS10 debugger and log prints show that values inside prism are corrupted
}

From what I understand, this means that somewhere in the code the shared_ptr reference count is messed up and a destructor is called, but I can't figure out where. Any ideas?

EDIT 1:

when breaking at the line return prism; at pressing Alt+Shift+F11 in VS10 (which shows all the function that are going to be called) I see both the boost::shared_ptr ctor and boost::shared_ptr dtor, which (I think) means I am returning the shared_ptr the wrong way. What is wrong there?

有帮助吗?

解决方案

Well, turns out it was a bug (not mine... wicked smile) in a whole different place. The members of the PointPrism class were saved as const reference, which made them delete outside the method that creates them... Thanks everyone for the help.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top