marshal_context 的 gcnew 是可选的吗?

0x1*_*son 6 .net memory-management c++-cli marshalling

我正在研究 ANSI C 库的 .NET 包装器。C .h 看起来像这样:

EXTERNC void foo_init(void);

EXTERNC char* foo_string_magic(const char *str);

EXTERNC void foo_cleanup(void);
Run Code Online (Sandbox Code Playgroud)

我的 C++/CLI 包装器如下所示:

public ref class FooWrapper abstract sealed
{
    private:    

        /// <summary>
        /// Provides an RAII context for native types lacking a destructor.
        /// </summary>
        static marshal_context ^_Context;    

        /// <summary>
        /// Marshals .NET String to native const char*. 
        /// </summary>
        static const char* _MarshalString(String ^str)
        {               
            return _Context->marshal_as<const char*>(str);
        }    

    public:    

        /// <summary>
        /// Initializes foo.
        /// </summary>
        static void Initialize()
        {
            _Context = gcnew marshal_context();             

            foo_init();
        }    

        /// <summary>
        /// This function should be called once you're done with using foo.
        /// </summary>
        static void Cleanup()
        {
            foo_cleanup();

            // uncommenting this line throws heap corruption error
            // delete _Context;
        }    

        /// <summary>
        /// Foo's magic!
        /// </summary>
        static String^ StringMagic(String ^str)
        {
            char *fooString = _MarshalString(str);

            return gcnew String(foo_string_magic(fooString));
        }
}
Run Code Online (Sandbox Code Playgroud)

我对 gcnew 与 marshal_context 的正确使用感到困惑。 MSDN使用gcnew来实例化marshal_context,而网上很多例子根本没有实例化marshal_context。

我的问题是:

  1. 在 marshal_context 中使用 gcnew 是可选的吗?如果是这样,为什么?
  2. 我的包装器由 C# 应用程序使用,该应用程序在调用 Cleanup() 时引发堆损坏错误。为什么?