C++ unique_ptr causes Application to crash
<p>I got a class with one static function. The purpose is to provide a common interface for the application to retrieve a logger object which will log to a specific file (assuming, for now, that a file can not be represented by different filepath notations). Each logger object is stored in a map with it's corresponding filename. Instead of creating a new logger if the same filename is passed again in the <code>config</code> object, the old logger is returned:</p>

<pre><code>typedef std::unique_ptr&lt;AbstractLogger&gt; LoggerPtr_t;
typedef std::map&lt;std::string, LoggerPtr_t &gt;::iterator LoggerMapIt_t;    
std::map&lt;std::string, LoggerPtr_t&gt; LoggerFactory::mLoggerMap;

LoggerPtr_t LoggerFactory::getGenericLogger(const LoggerConfig&amp; config){
    std::string filename = config.getFileName();    
    LoggerMapIt_t itLogger = mLoggerMap.find(filename);    
    if(itLogger == mLoggerMap.end()){
        mLoggerMap.insert(std::make_pair(filename, LoggerPtr_t(new SimpleLogger(config))));
        itLogger = mLoggerMap.find(filename);
    }    
    //if i uncommend the following 4 lines everything works fine
    if(itLogger != mLoggerMap.end()){
        return std::move(itLogger-&gt;second);
    }
    else
    return LoggerPtr_t(new SimpleLogger(config));    
}
</code></pre>

<p>But it seems that the application crashes if different threads try to write to the same logger. The file (means the <code>std::ofstream</code>) is protected by a mutex in the <code>log</code> method of <code>SimpleLogger</code>. I guess that the <code>unique_ptr</code> is the reason. Anyway I would like to have only one pointer to the logger object, because this objects behaves like a unique element (for each file only one logger).</p>

<p>Is it possible that the application crash is caused by the <code>unique_ptr</code>? Am I using the <code>unique_ptr</code> wrong? Is there a better solution to achieve my intention?</p>

<hr>

<p>EDIT: I got many good answers to this questions. Finally I adapted Jarod's Solution in my Code, but Joe's answer is also very usefull. </p>