Ok, I've been trying to wrap my head around this for some time now , but I dont get it can someone please tell me why case#1 throws an assert (BLOCK TYPE IS INVALID)?
case #1
mehodName()
{
// Get all dependents for this resource
boost::shared_ptr<std::set<std::string>> dependents = deactivatedResource->getDependendents();
// Do some stuff
} // Assertion thrown here (heap gets corrupted)
Here's the getDependents in this case :
boost::shared_ptr<std::set<std::string>> Resource::getDependendents()
{
return boost::shared_ptr<std::set<std::string>>(&dependents);
}
case #2
mehodName()
{
// Get all dependents for this resource
std::set<std::string>* dependents = deactivatedResource->getDependendents();
} // No problem !! (but an obvious leak , if I try to use delete ,then the same assertion as in case 1)
Here's the getDependents in this case :
std::set<std::string>* Resource::getDependendents()
{
return &dependents;
}
For both cases :
std::set<std::string> dependents;
dependents
an attribute of Resource
?, it seems that boost is trying to deallocate non-dynamic memory when the reference gets to zero. You could return a reference in that case.Update:
Then in your case it doesn't make sense returning a shared pointer as the object dependents
has not been created dynamically.
In any case, if you would need to create it dynamically, you should do the following:
In the class declaration:
boost::shared_ptr<std::set<std::string> > dependents;
In the constructor:
Constructor (...) : dependents (new std::set<std::string> ()) { ... }
But in your case there is no need to use dynamic memory. I would recommend you to return a reference rather than a pointer.