title: Why does my Interop code throw a "Stack cookie instrumentation code detected a stack-based buffer overrun" exception?
body: <p>Since I was messing a bit around with memory reading etc etc. And I made byte[] arrays with 1000000 elements so that they would store 1MB of data each. I wound up using around 750-isch of these 1000000 element array, which I added one by one when I retrieved data, eg: get MB of memory, add to list, get next MB. But it just failed with an overflow exception. So is there an actual limit of how much elements a List can contain, or is there a &quot;data&quot; limit to the List? If I didn't cross this limit what could have caused this problem to occur?</p>
  <p>EDIT: Screenshot of exception
  <a href="https://i.stack.imgur.com/FeIHQ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/FeIHQ.png" alt="enter image description here" /></a></p>
  <p>EDIT2: I am calling a function from a c++ dll that reads the next 1MB and returns a pointer to that array</p>
  <p>EDIT3:
  C# part</p>
  <pre><code>private static void FetchNextBuffer()
  {
      IntPtr pRaw = Wrapper.GetNextMB();
      byte[] buff = new byte[1000000];
      Marshal.Copy(buff, 0, pRaw, 1000000);
      RawDataFetch.Add(buff);
  }
  </code></pre>
  <p>wrapper</p>
  <pre><code>[DllImport(&quot;Dumper.dll&quot;)]
  public static extern IntPtr GetNextMB();
  </code></pre>
  <hr>
  c++ part
  .cpp file
  <pre><code>extern byte * __cdecl GetNextMB()
  {
      if (!VarsSet) SetVars();
      byte buffer[1000000];
      ReadProcessMemory(pHandle, (void*)Address, &amp;buffer, sizeof(buffer), 0);
      Address = Address + sizeof(buffer);
      return buffer;
  }
  </code></pre>
  <p>.h file</p>
  <pre><code>extern &quot;C&quot;
  {
      __declspec(dllexport) DWORD __cdecl GetPID();
      __declspec(dllexport)  byte * __cdecl GetNextMB();
  }
  </code></pre>
  <p>EDIT4: <strong>Thank you for all the insights and quick response guys (and girls if they are out there :S)</strong></p>
  <p>EDIT5: all fixed now and program is rolling</p>