title: std::map::insert exception
body: <p>This is another, "My code isn't working and i don't know why, " question i'm afraid. I just don't have enough knowledge of the stl to know why std::map::insert would throw an exception. If you know what cases it throws an exception, you can probably skip this wall of text and just answer. If you just desperately need some background on the issue, then have at it. I'll post my code and explain what is done, and i would be very grateful if all you with a better knowledge of the stl could explain what could be wrong with my call to insert.</p>
  
  <p>I wrote an object awhile ago that i use occasionally as my go to factory object. It's main purpose is basically to take a string and store both the string and a "create new object function" pointer, so that in the end, you can call a function, pass a string, and if there is a valid registration for it, it returns a new instance of a derived object. Less talk, more code, here's what i got:</p>
  
  <p>factory.h</p>
  
  <pre><code>#ifndef FACTORY_H
  #define FACTORY_H
  
  // library tools
  #include &lt;map&gt;
  #include &lt;string&gt;
  
  // Simplified registration macros
  #define DECLARE_DERIVED(T, base)    static Factory&lt;base&gt;::DerivedRegister&lt;T&gt; reg;
  #define DEFINE_DERIVED(T, base, s)  Factory&lt;base&gt;::DerivedRegister&lt;T&gt; T::reg(s); 
  
  template&lt;class base&gt;
  class Factory
  {
  protected:
      template&lt;class T&gt;
      static base * createT() { return new T;}
  
  public:
      typedef std::map&lt;std::string, base*(*)()&gt; map_type;
  
      virtual ~Factory(){ }
  
      static base * createInstance(const std::string &amp; s)
      {
          if(!m_Map.count(s))
              return nullptr;
          std::map&lt;std::string, base*(*)()&gt;::iterator it = m_Map.find(s);
          return it-&gt;second();
      }
  
      template &lt;class T&gt;
      struct DerivedRegister;
  
  protected:
      static map_type m_Map; 
  };
  
  template&lt;class base&gt;
  template&lt;class T&gt;
  struct Factory&lt;base&gt;::DerivedRegister : public Factory&lt;base&gt;
  {
      DerivedRegister(std::string const &amp; s)
      {
          m_Map.insert(std::pair&lt;std::string, base*(*)()&gt;(s, &amp;createT&lt;T&gt;));
      }
  };
  
  
  #endif
  </code></pre>
  
  <p>here's a better explanation of what it does real quick. Let's say you have a base class, class A . and then you have any number of derived classes. I make a factory object somewhere templated to A, and then either create a derived register object manually, or use the macro at the top within the derived classes declaration to create a static registry object. Then you define it in the implementation and call it's constructor, passing in a string to be used to identify the object. using the factory member createInstance you can pass in a string identifier and have a derived object returned, pointed to by an A *.</p>
  
  <p>example:</p>
  
  <p>A.h</p>
  
  <pre><code>class A
  {
  
  };
  </code></pre>
  
  <p>A.cpp</p>
  
  <pre><code>// the map for this factory template has to be defined somewhere, as it is static
  Factory&lt;A&gt;::map_type Factory&lt;A&gt;::m_Map;
  </code></pre>
  
  <p>b.h</p>
  
  <pre><code>#include &lt;A.h&gt;
  class B : public A
  {
    // anywhere in declaration of derived B
    DECLARE_DERIVED(A, B)
  };
  </code></pre>
  
  <p>b.cpp</p>
  
  <pre><code> // just somewhere in cpp file
   DEFINE_DERIVED(A, B, "B")
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>int main()
  {
    A * ptr;
    Factory&lt;A&gt; factory;
  
    ptr = factory.createInstance("B");
  }
  </code></pre>
  
  <p>This object has worked for me in the past, mostly without a hitch. Now i'm doing a project a little more complicated. I've taken a liking to the data organization/ api design involved with game engines, and i'm just trying to implement a solution of cataloging, (but not instantiated) shaders, so that you have a whole list of the shaders you've programmed, but they will not be instantiated at run-time unless needed. That aside, this question actually has nothing to do with d3d11, or at least i hope not. </p>
  
  <p>So here is what's going on. I have an object that represents a graphics-shader abstract class. All the shaders you wish to write must derive from this object. The you derive from and implement it's functions differently for all your different shaders.</p>
  
  <p>let's call the base object "SYNC::D3D11Shader" in namespace sync and the derived shaders "ColorShader" "LightShader" and "TextureShader". Since i do not simply want to make an std::map of instances of these shaders within the rendering object, i make a factory within the rendering object like this.</p>
  
  <p>D3D11Renderer.h</p>
  
  <pre><code>class D3D11Renderer
  {
      // many other members...
      Factory&lt;D3D11Shader&gt; m_ShaderFactory;
      // many other member...
  };
  </code></pre>
  
  <p>D3D11Renderer.cpp</p>
  
  <pre><code>// define this templated classes map or you'll get undefined errors
  Factory&lt;SYNC::D3D11Shader&gt;::map_type Factory&lt;SYNC::D3D11Shader&gt;::m_Map;
  </code></pre>
  
  <p>and then in the ColorShader i use the macros like so</p>
  
  <p>D3D11ColorShader.h</p>
  
  <pre><code>class D3D11ColorShader : public SYNC::D3D11Shader
  {
    // ...lotsa members
    DECLARE_DERIVED(D3D11ColorShader, SYNC::D3D11Shader)
    // lotsa member...
  };
  </code></pre>
  
  <p>D3D11ColorShader.cpp</p>
  
  <pre><code>// define the registery object with it's key here
  DEFINE_DERIVED(D3D11ColorShader, SYNC::D3D11Shader, "ColorShader")
  </code></pre>
  
  <p>this all compiles fine, and where it throws it's exception is where i first call the registryObjects constructor in D3D11ColorShader.cpp, spefically at the insert call. the exception error is this:</p>
  
  <blockquote>
    <p>Unhandled exception at 0x772315de in Syncopate.exe: 0xC0000005: Access
    violation reading location 0x00000004.</p>
  </blockquote>
  
  <p>So in reality, the question boils down to, when does std::map::insert throw an exception and why. I just knew everyone would be asking for some background on what i'm doing. Low and behold, a giant wall of text has appeared! All i really need is a hunch.</p>
  
  <p>also should i or should i not tag d3d11, because the question doesn't really pertain to it?</p>