calling C++ from C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • TamaK
    New Member
    • Jan 2009
    • 1

    calling C++ from C#

    Hello,

    I have unmanaged C++ dll, that I use in a managed code (C++). I call this managed code from C#. It seems to work fine, but I am not sure about the memory management, should I free the data? Here is sample of my code:

    #pragma unmanaged

    void unmanaged_free (void *p_)
    {
    free (p_);
    }
    void *unmanaged_mall oc (int size_)
    {
    return malloc (size_);
    }

    #pragma managed

    void Dnzmq::send (int eid_, void * data_, size_t size_)
    {
    // Copy data to the unmanaged buffer.
    void *data = unmanaged_mallo c (size_);
    assert (data);
    memcpy (data, data_, size_);

    // Forward the call to native 0MQ library.
    zmq::message_t msg (data, size_, unmanaged_free) ;
    context->api_thread->send (eid_, msg);
    }
    I call send() function from C#. My question is: who deallocates the function argument data_ ? Is it correct to free *data, that was allocated in unmanaged part of the code?
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    All memory that has been allocated needs to be freed apart from memory that is allocated with gcnew (which is automatically garbage collected for you).

    However the zmq::message_t class (structure?) appears to take a pointer to function used to free the data suggesting that may be it is going to call the free function itself automatically for you.

    You need to understand exactly how the zmq::message_t class works in order to understand why you are passing a pointer to the free function and what the class does with that pointer.

    I have to say it looks like poor programming since the class ought to be able to take the original pointer to the data and handle the malloc and free itself rather than have the caller call malloc and pass the free function pointer to the class.

    Comment

    • vekipeki
      Recognized Expert New Member
      • Nov 2007
      • 229

      #3
      1. You should not free data_, since it is being passed from the managed side, where it was allocated. During P/Invoke .NET marshaller will pin your managed object so it doesn't get moved by the GC. If your unmanaged function saves a reference to your pointer, or runs in background, then you must pin your object manually (using a GCHandle) and release it when needed.

      If your object's managed representation is not the same as native (you are using the MarshalAsAttrib ute all over the place), then marshaller will create a copy of your object which matches the native representation, and also free it after use automatically.

      In any situation, this object can be considered managed during entire call from c#, so you don't have to free it.

      2. It seems like you are passing a pointer to unmanaged_free( ) in your message, if this means that unmanaged_free( ) will get called at the end with data passed as parameter, then I guess you don't need to free it. This part should be well understood before deciding it. At least create a small test app and watch if your process grows in Task Manager :)

      Comment

      Working...