Interop between C++ and C# via C++/CLI with callbacks I want to use an existing C++ library in a C# app. The C++ library will have async callbacks for things like OnConnect and user updates so I want to reflect this in the eventual C# code. I would appreciate thoughts on the following implementation particularly in the area of memory leaks in the CLI part.

To begin, I have a simple C++ class:

#include &lt;string&gt;

class CppClass
{
public:

    typedef void(*Callback)(int);

    CppClass(){};
    ~CppClass(){};

    void Connect(std::string conn_info)
    {
        OnConnect();
    };

    void OnConnect()
    {
        m_callback(42);
    };

    void SetOnCallback(Callback callback)
    {
        m_callback = callback;
    };

private:
    Callback m_callback;
};


To communicate with this, I have a C++/CLI wrapper:

using namespace System;
using namespace System::Runtime::InteropServices;

namespace CliCpp {

    public delegate void OnConnectDelegate(int);

    public ref class WrapperClass
    {
    public: 

        // C# callback delegates
        CliCpp::OnConnectDelegate^ OnConnectHandler;

        // Ctor / dtor
        WrapperClass()
        {
            m_native = new CppClass();

            // Set OnConnect callback
            CliCpp::OnConnectDelegate^ managed_on_connect = gcnew CliCpp::OnConnectDelegate(this, &amp;WrapperClass::OnConnect);
            IntPtr stub_ptr = Marshal::GetFunctionPointerForDelegate(managed_on_connect);
            CppClass::Callback fptr = static_cast&lt;CppClass::Callback&gt;(stub_ptr.ToPointer());
            m_native-&gt;SetOnCallback(fptr);
            GC::KeepAlive(managed_on_connect);
        };

        ~WrapperClass()
        {
            this-&gt;!WrapperClass();
        };

        !WrapperClass()
        {
            if (m_native != nullptr)
                delete m_native;
        };

        // Methods
        void Connect(String^ conn_info)
        {
            m_native-&gt;Connect(msclr::interop::marshal_as&lt;std::string&gt;(conn_info));
        };

        // Event handler from C++ code
        void OnConnect(int i)
        {
            // Call C# code:
            OnConnectHandler(i);
        };      

    private:
        CppClass* m_native;     
    };
}


And finally, I have some C# calling code:

using CliCpp;

namespace Caller
{
    class Program
    {   
        static void Main(string[] args)
        {
            using (WrapperClass cls = new WrapperClass())
            {
                // Set up the C# callback
                cls.on_connect_handler = OnConnect;
                cls.Connect("Hello");
                Console.ReadLine();
            }
        }

        // Callback Function
        static void OnConnect(int i)
        {
            Console.WriteLine("C# handler called!");
        }

    }
}


So, what I would like an opinion on is:


Is this implementation of callbacks good practice?
In the C++/CLI constructor where I set up the callback, am I using gcnew and 'GC::KeepAlive(x)' correctly so as not to leak (do I need a corresponding delete etc.)? 


Any input appreciated!
I'm not sure about the callbacks but you are using the destructor/finalizer in a way that might cause problems.


   ~WrapperClass()
    {
        this-&gt;!WrapperClass();
    };

    !WrapperClass()
    {
        if (m_native != nullptr)
            delete m_native;
    };



If you are doing this then the descructor should call GC.SuppressFinalize(this); because the finalizer has already been called.

Then in your finalizer you should set the m_native = nullptr; because if the GC calls it again later it will crash as the object has been deleted but it's still pointing to it.

    !WrapperClass()
    {
        if (m_native != nullptr)
        {
            delete m_native;
            m_native = nullptr;
        }
    }


As to the m_native I'd call it _nativePtr.