Boost logo

Boost Users :

From: todd (todd_at_[hidden])
Date: 2004-02-07 19:56:23


On Sat, 07 Feb 2004 23:12:58 -0600, Joshua Little wrote
> LoggerMap::iterator i = loggers.find(logger);
> if (!(*i).second.get()) {

If logger doesn't already exist in loggers, then i will be loggers.end().
Dereferencing end() is undefined. Use this for your conditional instead:

if(i != loggers.end())

Or, if you want to gain speed, do this:

boost::shared_ptr<Logger> LoggerFactory::getInstance(std::string& logger)
{
  boost::shared_ptr<Logger>& TheLoggerRef = loggers[logger];
  if(!TheLoggerRef.get())
    TheLoggerRef.reset(new Logger());
  return TheLoggerRef;
}

This version searches the map only once.

find() doesn't add entries, but the array operator does.

Todd


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net