libgit2/libgit2sharp — pull request #291 by phkelley (2013-01-08 13:20:30+00:00)
Title: Improve perf and correctness of UTF-8 marshaling

When marshaling managed String (or FilePath) instances to libgit2, we weren't cleaning up the allocated native buffers that hold the UTF-8 copy of the string. Given how often we do this, this is a pretty significant memory leak.

We can only clean up these native buffers appropriately if we distinguish between the common case of marshaling Strings from managed to native as part of a function call (where we do need to clean up the native buffer), and the less common case of marshaling a libgit2-owned native string to managed. This distinction is now made with the "NoCleanup" versions of the marshalers. You would most often use a "NoCleanup" marshaler for the return value of a function.

While I was working in these files, I also rewrote the marshaling logic itself to do less work.

This change was tested on Windows with full page heap enabled in all possible CLR combinations (x86, amd64, CLR 2.0, CLR 4.0), and on Linux with Mono.