Friday, October 30, 2009

Boost smart_pointers need a new object not a pointer

typedef std::string TString;
typedef boost::smart_ptr RefString;

// this does not work->nasty memory leak
RefString rsUnitName = RefString(new std::string((*i)->pType->unitType));
RefString rsInstanceName = RefString(new std::string((*i)->unitObjectName));

refMapUnitNameUnitInstName.get()->operator [](rsUnitName) = rsInstanceName;

refMapUnitNameUnitInstName->insert(std::make_pair(rsUnitName, rsInstanceName));

refMapUnitNameUnitInstName->insert(std::pair(rsUnitName, rsInstanceName));

// this also causes a memory leak.
// pair can't figure out the size of the objects pointed to by the RefString for some reason
// and assigns a default of 8-bits

refMapUnitNameUnitInstName->insert(std::pairpType->unitType)),
RefString(new std::string((*i)->unitObjectName)))
);


// THIS WORKS!
// New the objects inside of the smart pointer wrapper that passed the types to std:pair
refMapUnitNameUnitInstName->insert(std::pairpType->unitType)),
RefString(new std::string((*i)->unitObjectName)))
);