Stroring local variable in std::map
<p>I have a class Message and a class Cache.
In Message::processMessage() fn. I create a instance of another class CacheRef(not shown below.)
then I call Cache::cacheData(cacheRef)</p>

<p>Now, in Cache class, I have a map which has its key as CacheReference. I store the ref that I passed to cacheData fn. in this map.</p>

<pre><code>class Message
{
private:
Key m_key;

public:
  void processMessage(int a, int b, Cache *pCache)
   {
      CacheRef ref(a, b, m_key); //CacheRef is a class defined in same file
      //some char *data - do processing an dfill it!!
      pCache-&gt;cacheData(ref, data);

    }

}

class Cache
{
  public:
   void cacheData(CacheRef &amp;ref, const char* data)
   {
     CacheDir *dir;
     std::map&lt;&lt;CacheRef, CacheDir*&gt;::iterator it = m_dirs.find(ref);

      if(it == m_dirs.end())
     {
        dir = new CacheDir();
        m_dirs.insert(ref, dir);

      }
    }

    std::map&lt;CacheRef, CacheDir*&gt; m_dirs; //CacheDir is some class defined in the same file
}
</code></pre>

<p>Now, the code is working absolutely fine. But I have this concern(not sure!!) that I am storing some local variable in map, which which cease to exist as soon as processMessage()fn. exits. So, am I accessing some invalid memory, is it just by luck that this code is working.</p>

<p>If this is wrong, what is the best way to achieve this behaviour?
I don't have boost on my system, so can't use shared_ptr for anything.</p>
