title: Unexplained memory leak with Marshal.StructureToPtr
body: <p>I'm developing an application which involves native C++ to C# interop via a C++/CLR wrapper.</p>
  
  <p>I'm having trouble with the following operation, which causes a memory leak:</p>
  
  <pre><code>MyObject data = (MyObject)Marshal.PtrToStructure(ptr, typeof(MyObject));
  Marshal.StructureToPtr(data, ptr, false);
  </code></pre>
  
  <p>(NOTE: I realise that i'm not actually doing anything to "data" at this time, so this is superfluous.)</p>
  
  <p>Memory usage continues rising until the app crashes due to the system running out of memory. When I remove this code, this doesn't happen. It's not the garbage collector since a) it should collect when the system gets low on memory and b) i've tried forcing it with GC.Collect().</p>
  
  <p>In fact, I've narrowed the leak down to the StructureToPtr command.</p>
  
  <p>I'm unable to set the third parameter to "true", since the memory was allocated by native C++, and C# considers this "protected" memory that cannot be freed.</p>
  
  <p>I've checked that the populated Data struct is intact, has valid data, and is the same size as the equivalent native struct.</p>
  
  <p>In my mind, this is what should be happening:</p>
  
  <ol>
  <li><p>My native structure referenced by ptr is marshalled and copied to the "data" managed structure</p></li>
  <li><p>The manage structure is copied back to the same memory referenced by ptr.</p></li>
  </ol>
  
  <p>I can't see how this can cause a memory leak, because it's exactly the same size of structure, being copied back into the same memory space. But clearly it does, removing the code plugs the leak.</p>
  
  <p>Is there some mechanic here which i'm not understanding correctly?</p>
  
  <p>Edit: As requested, here are the declarations for "MyObject".</p>
  
  <p>C#:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct MyObject
  {
      [MarshalAs(UnmanagedType.I1)]
      public bool ParamOne;
      [MarshalAs(UnmanagedType.I1)]
      public bool ParamTwo;
      [MarshalAs(UnmanagedType.I1)]
      public bool ParamThree;
      [MarshalAs(UnmanagedType.I1)]
      public bool ParamFour;
      [MarshalAs(UnmanagedType.I1)]
      public bool ParamFive;
      [MarshalAs(UnmanagedType.I1)]
      public bool ParamSix;
      [MarshalAs(UnmanagedType.R4)]
      public float ParamSeven;
      [MarshalAs(UnmanagedType.R4)]
      public float ParamEight;
      [MarshalAs(UnmanagedType.R4)]
      public float ParamNine;
      public Vector2f ParamTen;
      public Vector2f ParamEleven;
      [MarshalAs(UnmanagedType.LPWStr)]
      public string ParamTwelve;
      [MarshalAs(UnmanagedType.LPWStr)]
      public string ParamThirteen;
      [MarshalAs(UnmanagedType.LPWStr)]
      public string ParamFourteen;
      public IntPtr ParamFifteen;
      public IntPtr ParamSixteen;
  }
  </code></pre>
  
  <p>C++:</p>
  
  <pre><code>struct MyObject
  {
      public:
      bool ParamOne;
      bool ParamTwo;      
      bool ParamThree;
      bool ParamFour;
      bool ParamFive;
      bool ParamSix;
      float ParamSeven;
      float ParamEight;
      float ParamNine;
      Vector2f ParamTen;
      Vector2f ParamEleven;
      wchar_t * ParamTwelve;
      wchar_t * ParamThirteen;
      wchar_t * ParamFourteen;
      void * ParamFifteen; 
      void * ParamSixteen;
  };
  </code></pre>
  
  <p>The definition of Vector2f is as follows:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  public struct Vector2f
  {
      [MarshalAs(UnmanagedType.R4)]
      float x;
      [MarshalAs(UnmanagedType.R4)]
      float y;
  }
  </code></pre>