title: Native Plugin Null Exception OSX
body: <p>I've cross-posted this on the Unity forums; apologies if this breaks conduct.</p>
  
  <p>Hi folks</p>
  
  <p>I've come across a crash that happens when I try to use an std::map in C++ from Unity through a plugin.
  I am not passing the contents of the map to Unity from the plugin; I am simply calling member functions such as the find() function (where my application crashes)</p>
  
  <p>The code executes fine when I load my plugin into a C++ console application but it crashes in Unity.
  I suspect there is a marshalling/memory allocation issue where Unity is not allowing me to allocate memory of some sort</p>
  
  <p>Attached is the Player.log output and the calling Unity script and the callee C++ code.
  Any help would be much appreciated.</p>
  
  <p>Regards</p>
  
  <p>Lancophone</p>
  
  <p>Calling C# code:</p>
  
  <pre><code>public string filename; // The name of the audio file this behaviour corresponds to
  
  [DllImport ("Unity-Audio-Plugin")]
  private static extern void addSoundToRendererBank(string filename);
  
  [DllImport ("Unity-Audio-Plugin")]
  private static extern void addSoundToRendererBankAtPosition(string filename, float xPos, float zPos);
  
  [DllImport ("Unity-Audio-Plugin")]
  private static extern bool setSoundWithNameToPosition (string filename, float xPos, float zPos);
  
  [DllImport ("Unity-Audio-Plugin")]
  private static extern bool deactivateSoundInRendererBank (string filename);
  
  // Use this for initialization
  public int incr;
  void Start () {
      incr = 1;
      addSoundToRendererBank (filename);
      InvokeRepeating ("moveMe", 0, 0.3F);
  }
  .....
  </code></pre>
  
  <p>Callee C++ code:</p>
  
  <pre><code> void addSoundToRendererBank(char *filename)
   {
       printf("Calling the addSoundToRendererBank function with %s as a filename\n", filename);
       copyStringFromManagedCaller(filename);
       std::string fileNameStr(&amp;buffer[0]);
  
       int soundLoc = soundBank-&gt;getIDForName(fileNameStr);
       if (soundLoc &gt; 0) { // Sound is already in the bank
           // If its just been deactivated, reactivate it
           std::string key(&amp;buffer[0]);
           soundBank-&gt;activateSoundSource(key);
       }
       ...
       ...
       int SoundBank::getIDForName(std::string &amp;filePath)
       {
            // Filepath -&gt; sourceIDMap -&gt; source -&gt; ID
            std::string key(filePath);
            int location;
  
            std::cerr &lt;&lt; "About to enter the try-catch block in the SoundBank::getIDForName() function using " &lt;&lt; key &lt;&lt; std::endl;
  
            // Print the current elements inside the map
            std::cerr &lt;&lt; "The contents of the sourceIDMap:" &lt;&lt; std::endl;
            SoundSourceMapIterator map_it;
          //        for (map_it = sourceIDMap.begin(); map_it != sourceIDMap.end(); ++map_it) {
          //            std::cerr &lt;&lt; "Key: " &lt;&lt; map_it-&gt;first &lt;&lt; "\tValue: " &lt;&lt; map_it-&gt;second &lt;&lt; std::endl;
          //        }
            std::cerr &lt;&lt; "I'm about to die.... :-(" &lt;&lt; std::endl;
  
            // Throws randomers; using find instead
            //        try
            //        {
            //            location = sourceIDMap.at(key);
            //        }
            //        catch (const std::out_of_range&amp; oorEx)
            //        {
            //            location = -1;
            //        }
  
            SoundSourceMapIterator it = sourceIDMap.find(key);
            if (it == sourceIDMap.end())
                location = -1;
            else
                location = it-&gt;second;
            .......
  </code></pre>
  
  <p>Console output:</p>
  
  <blockquote>
    <p>.....<br>
    The contents of the sourceIDMap:<br>
    I'm about to die.... :-(<br>
    Receiving unhandled NULL exception<br>
    Obtained 27 stack frames.<br>
    &#35;0 0x00000111a00b91 in std::__1::__tree_iterator, std::__1::allocator >, int>, std::__1::__tree_node, std::__1::allocator >, int>, void*>*, long> std::__1::__tree, std::__1::allocator >, int>, std::__1::__map_value_compare, std::__1::allocator >, std::__1::__value_type, std::__1::allocator >, int>, std::__1::less, std::__1::allocator > >, true>, std::__1::allocator, std::__1::allocator >, int> > >::find, std::__1::allocator > >(std::__1::basic_string, std::__1::allocator > const&amp;)<br>
    &#35;1 0x000001119fdb21 in <br>
    uap::SoundBank::getIDForName(std::__1::basic_string, std::__1::allocator >&amp;)<br>
    &#35;2 0x00000111a06891 in addSoundToRendererBank<br>
    ......</p>
  </blockquote>