title: 0xC0000005: Access violation reading location 0xffffffffffffffff
body: <p>I have:</p>
  
  <ul>
  <li>An unmanaged C++ app</li>
  <li>A C++/CLI Wrapper</li>
  <li>A C# GUI</li>
  </ul>
  
  <p>I am seeing this crash occur only in Release and not debug. The crash also does not occur on neither debug or release when the unmanaged C++ app is run by itself.</p>
  
  <p>All I can say is the crash occurs in this line of code:</p>
  
  <pre><code>            if ((std::find(vstrEEDRRMachines.begin(), vstrEEDRRMachines.end(), m_sFrame.strSourceAddress) != vstrEEDRRMachines.end()
                  &amp;&amp; std::find(vstrEEDRRMachines.begin(), vstrEEDRRMachines.end(), m_sFrame.strDestAddress) != vstrEEDRRMachines.end())
                  || (std::find(vstrRRRHMachines.begin(), vstrRRRHMachines.end(), m_sFrame.strSourceAddress) != vstrRRRHMachines.end()
                  &amp;&amp; std::find(vstrRRRHMachines.begin(), vstrRRRHMachines.end(), m_sFrame.strDestAddress) != vstrRRRHMachines.end())) 
              {
                  // Create appropriate buffer size for raw message (i.e. size of payload along with the extra padding
                  // for decoding)
                  m_sFrame.iMessageSize = m_sFrame.iPayloadLength;
                  m_sFrame.iOriginalMessageSize = m_sFrame.iPayloadLength;
                  m_sFrame.pszMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
                  m_sFrame.pszOriginalMessage = new unsigned char[m_sFrame.iPayloadLength + Constants::RSSL_DECODE_PADDING];
              }
  </code></pre>
  
  <p>Although I can't see what's inside vstrEEDRRMachines and vstrRRRHMachines (because we are in release), I can see when in the original C++/CLI wrapper that there are valid string entries in the vector.</p>
  
  <p>The stack trace is as follows:</p>
  
  <blockquote>
    <p>msvcr100.dll!0000000069abbdc0()         [Frames below may be incorrect
    and/or missing, no symbols loaded for msvcr100.dll]<br>
      DataVerifier.exe!std::_Find,std::allocator</p>
    
    <blockquote>
      <ul>
      <li>__ptr64,std::basic_string,std::allocator
      
      <blockquote>
        <p>(std::basic_string,std::allocator > * _First, std::basic_string,std::allocator > *
        _Last, const std::basic_string,std::allocator > &amp;
        _Val)  Line 41 + 0x4a bytes   C++
        DataVerifier.exe!DataVerifier::CPCAPParser::Parse(const char * szFileName,
        std::vector
        &amp; vSFramesRWF1Messages, std::vector,std::allocator
        ,std::allocator,std::allocator</p>
        
        <blockquote>
          <ul>
          <li>vstrEEDRRMachines, std::vector,std::allocator
          ,std::allocator,std::allocator</li>
          <li>vstrRRRHMachines, RsslDataDictionary &amp; rsslDataDictionary)  Line 178 + 0x19 bytes C++     [External Code]<br>
          DataVerifierLib.dll!DataVerifierLib::PCAPParserWrapper::ParseWrapper(System::String^
          strInputFileNames) Line 136 + 0xf6 bytes  C++ 
          DataVerifierGUI.exe!DataVerifierGUI.Form1.button1_Click(object
          sender, System.EventArgs e) Line 42 + 0x30 bytes  C# 
          user32.dll!00007fff7a8c250d()       user32.dll!00007fff7a8c2367()<br>
          System.Windows.Forms.ni.dll!00007fff535368c0()</li>
          </ul>
        </blockquote>
      </blockquote></li>
      </ul>
    </blockquote>
  </blockquote>
  
  <p>The crash specifically occurs in c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\algorithm in this part of the code:</p>
  
  <pre><code>        // TEMPLATE FUNCTION find
  template&lt;class _InIt,
      class _Ty&gt; inline
      _InIt _Find(_InIt _First, _InIt _Last, const _Ty&amp; _Val)
      {   // find first matching _Val
      for (; _First != _Last; ++_First)
          if (*_First == _Val) &lt;-- CRASH OCCURS HERE
              break;
      return (_First);
      }
  </code></pre>
  
  <p>I have no idea what's going on, since release mode with C# + C++/CLI + C++ apps is where the crash is occurring. Is there any way I can easily fix this?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>It seems like this occurs 9 times out of 10, but i've noticed the times it does work, when I check the parameters of the Parse function just as it goes into the function (no lines are executed yet), all 3 vectors have normal values (the 1st one is 0, the second and third contain IP addresses as expected). However, most of the time, as soon as we get into the Parse function, all 3 vectors have negative size and capacities... and when they are used, everything goes boom. I'm going to past the entrance of the Parse function and also the C++/CLI wrapper before it.</p>
  
  <pre><code>bool CPCAPParser::Parse(const char* szFileName, std::vector&lt;CRWFCapsule&gt; &amp;vSFramesRWF1Messages, std::vector&lt;std::string&gt; vstrEEDRRMachines, std::vector&lt;std::string&gt; vstrRRRHMachines, RsslDataDictionary &amp;rsslDataDictionary) // change to a vector of rwf1capsules
  {
  </code></pre>
  
  <p>This is the C++/CLI Wrapper:</p>
  
  <pre><code>// This is the main DLL file.
  
  #include "stdafx.h"
  
  #include "DataVerifierLib.h"
  
  // Constructor Implementation
  DataVerifierLib::PCAPParserWrapper::PCAPParserWrapper()
  {
  }
  
  std::vector&lt;std::string&gt; DataVerifierLib::PCAPParserWrapper::CreateNewMachineCollection(std::vector&lt;std::string&gt; vstrNewMachines, std::vector&lt;std::string&gt; vstrMachine1, std::vector&lt;std::string&gt; vstrMachine2)
  {
      vstrNewMachines.reserve(vstrMachine1.size() + vstrMachine2.size()); // preallocate memory
      vstrNewMachines.insert(vstrNewMachines.end(), vstrMachine1.begin(), vstrMachine1.end());
      vstrNewMachines.insert(vstrNewMachines.end(), vstrMachine2.begin(), vstrMachine2.end());
      return vstrNewMachines;
  }
  
  bool DataVerifierLib::PCAPParserWrapper::ParseWrapper(String^ managedString)
  {
      // String conversion from c# to c++
      String^ managedStringTmp = managedString;
      std::string strInputFileNames = msclr::interop::marshal_as&lt;std::string&gt;(managedStringTmp);
  
      std::vector&lt;std::string&gt; vRRMachines;
      std::vector&lt;std::string&gt; vRHMachines;
      std::vector&lt;std::string&gt; vEEDMachines;
      std::vector&lt;std::string&gt; vPorts; // decide on what checks are to be made. Should frame have matching src/dest ports? or just one of them.
      std::vector&lt;std::string&gt; vEEDRRMachines;
      std::vector&lt;std::string&gt; vRRRHMachines;
      std::vector&lt;std::string&gt; vstrLines;
  
      std::string strTxtFile = "ServerIPList.txt"; //argv[2]; // ServerIPList.txt
      std::string strLine;
      std::ifstream in(strTxtFile);
  
      if (!in)
      {
          std::cout &lt;&lt; "There was a problem opening the file." &lt;&lt; std::endl;
          std::cerr &lt;&lt; "Error: " &lt;&lt; strerror(errno) &lt;&lt; std::endl;
          return -1;
      }
  
      while (std::getline(in, strLine))
      {
          vstrLines.push_back(strLine);
      }
  
      for (int i = 0; i &lt; vstrLines.size(); ++i)
      {
          if (vstrLines[i].substr(0, 2) == "rr")
          {
              boost::split(vRRMachines, vstrLines[i], boost::is_any_of(","));
              if (vRRMachines.size() &lt; 2)
                  return -1;
              else
                  vRRMachines.erase(vRRMachines.begin());
          }
          else if (vstrLines[i].substr(0, 2) == "rh")
          {
              boost::split(vRHMachines, vstrLines[i], boost::is_any_of(","));
              if (vRHMachines.size() &lt; 2)
                  return -1;
              else
                  vRHMachines.erase(vRHMachines.begin());
          }
          else if (vstrLines[i].substr(0, 3) == "eed")
          {
              boost::split(vEEDMachines, vstrLines[i], boost::is_any_of(","));
              if (vEEDMachines.size() &lt; 2)
                  return -1;
              else
                  vEEDMachines.erase(vEEDMachines.begin());
          }
          else if (vstrLines[i].substr(0, 5) == "ports")
          {
              boost::split(vPorts, vstrLines[i], boost::is_any_of(","));
              if (vPorts.size() &lt; 2)
                  return -1;
              else
                  vPorts.erase(vPorts.begin());
          }
      }
  
      // Create a vector with EED/RR and RR/RH combined addresses
      vEEDRRMachines = CreateNewMachineCollection(vEEDRRMachines, vEEDMachines, vRRMachines);
      vRRRHMachines = CreateNewMachineCollection(vRRRHMachines, vRRMachines, vRHMachines);
  
      // Initialise Rssl
      RsslRet rsslRet;
      RsslError rsslError;
      rsslRet = rsslInitialize(RSSL_LOCK_NONE, &amp;rsslError);
  
      if (rsslRet != RSSL_RET_SUCCESS)
          return -1;
  
      // Initialise Field Dictionary
      // To prevent memory issues, we need to use "malloc" for the data dictionary in order to load RWF.dat and enumtype.def
      RsslDataDictionary *rsslDataDictionary = (RsslDataDictionary*)malloc(sizeof(RsslDataDictionary));
      RsslBuffer rsslBufferError;
      rsslClearDataDictionary(rsslDataDictionary);
      if (rsslRet = rsslLoadFieldDictionary("RWF.DAT", rsslDataDictionary, &amp;rsslBufferError) != RSSL_RET_SUCCESS)
      {
          // Ensure we free the data dictionary memory when finished
          free(rsslDataDictionary);
          //if (iDisplayType != DISPLAY)
          //  std::cout &lt;&lt; "Could not load RDM Field Dictionary file." &lt;&lt; std::endl;
          return -1;
      }
  
      // Load enum dictionary
      if (rsslLoadEnumTypeDictionary("enumtype.def", rsslDataDictionary, &amp;rsslBufferError) != RSSL_RET_SUCCESS)
      {
          // Ensure we free the data dictionary memory when finished
          free(rsslDataDictionary);
          //if (iDisplayType != DISPLAY)
          //  std::cout &lt;&lt; "Could not load Enum Type Dictionary file." &lt;&lt; std::endl;
          return -1;
      }
  
      std::string strCombinedFileName;
      std::vector&lt;CRWFCapsule&gt; vRWFCapsules;
      std::vector&lt;std::string&gt; vstrInputFileNames;
      pPcapParser = new CPCAPParser(1,1); // Initiate C++ class instance
      boost::algorithm::split_regex(vstrInputFileNames, strInputFileNames, boost::regex(","));
  
      // Let's iterate through each PCAP file and parse it.
      for (int i = 0; i &lt; vstrInputFileNames.size(); ++i)
      {
          if (false == strCombinedFileName.empty())
          {
              strCombinedFileName.append("-");
          }
  
          if (false == pPcapParser-&gt;Parse(vstrInputFileNames[i].c_str(), vRWFCapsules, vEEDRRMachines, vRRRHMachines, *rsslDataDictionary))
          {
              delete pPcapParser;
              // Ensure we free the data dictionary memory when finished
              free(rsslDataDictionary);
              vRWFCapsules.clear();
              return -1;
          }
  
          strCombinedFileName = strCombinedFileName.append(pPcapParser-&gt;GetFileName());
      }
  
      //if (iDisplayType != NO_DISPLAY)
      //  std::cout &lt;&lt; "Clearing up..." &lt;&lt; std::endl;
      delete pPcapParser;
  
      if (rsslRet = rsslDeleteDataDictionary(rsslDataDictionary) != RSSL_RET_SUCCESS)
      {
          // Ensure we free the data dictionary memory when finished
          free(rsslDataDictionary);
          //if (iDisplayType != NO_DISPLAY)
          //  std::cout &lt;&lt; "Problem deleting dictionary." &lt;&lt; std::endl;
          return -1;
      }
  
      // Ensure we free the data dictionary memory when finished
      free(rsslDataDictionary);
  
      if (vRWFCapsules.empty())
          return -1;
  
      CMessageSorter msgSorter(vEEDMachines, vRRMachines);
      std::map&lt;std::string, SMessageInfo&gt; m_msMessageInfo;
      std::map&lt;std::string, SSystemInfo&gt; m_msSystemInfo = msgSorter.SortMessages(vRWFCapsules, 1, m_msMessageInfo);
      if (m_msSystemInfo.empty())
          return -1;
  
      CDataVerifier dataVerifier(strCombinedFileName, 1, 1);
      dataVerifier.CreateOutputForAllMessages(m_msMessageInfo);
      dataVerifier.Process(m_msSystemInfo);
  
      // When clearing the vector, we have to make sure that the destructor of the RWF message is being called... so everything destroys itself correctly.
      // Will need to check if destruction is down properly. However, how can we delete RWF Capsules encapsulated in eachother?...
  
      vRWFCapsules.clear();
  }
  </code></pre>
  
  <p>And its appropriate header file:</p>
  
  <pre><code>// DataVerifierLib.h
  
  #pragma once
  #include "../DataVerifier/CRWFCapsule.h"
  #include "../DataVerifier/ISystem.h"
  #include "../DataVerifier/CFrameAnalyser.h"
  #include "../DataVerifier/CPCAPParser.h"
  #include "../DataVerifier/CMessageSorter.h"
  #include "../DataVerifier/CDataVerifier.h"
  #include "../DataVerifier/CSourceDirectoryResponse.h"
  #include "../DataVerifier/CDataVerifierConstants.h"
  #include &lt;vector&gt;
  #include &lt;fstream&gt;
  #include &lt;sstream&gt;
  #include &lt;stdexcept&gt;
  #include &lt;boost/algorithm/string.hpp&gt;
  #include &lt;msclr/marshal_cppstd.h&gt;
  
  using namespace System;
  using namespace DataVerifier;
  
  namespace DataVerifierLib {
  
      public ref class PCAPParserWrapper
      {
          // TODO: Add your methods for this class here.
      public:
          // Constructor
          PCAPParserWrapper();
  
          // Wrapper Methods
          bool ParseWrapper(String^ strInputFileNames);
  
          // Fill vectors with appropriate RR/EED/RH addresses
          std::vector&lt;std::string&gt; CreateNewMachineCollection(std::vector&lt;std::string&gt; vstrNewMachines, std::vector&lt;std::string&gt; vstrMachine1, std::vector&lt;std::string&gt; vstrMachine2);
  
          // Public Variable
          double initVal;
  
      private:
          CPCAPParser *pPcapParser; // an instance of class in C++
      };
  }
  </code></pre>