none
preventing relocation of delegate passed to native code RRS feed

  • Question

  • hi,

     i have a delegate in C# which is called by native code now and then say every one hour. it is continuous process . i just registered the

    delegate only at start up time.

    and the code is working fine.

    my question is if garbage collector relocates the delegate object ( not collecting) how the native code calls that c# delegate.

    bye 

    yln

     

    Wednesday, December 15, 2010 5:19 PM

Answers

  • If the delegate itself gets collected, you've got a big problem.  You need to design your app so that this doesn't happen.  For example, see http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx which shows one way to prevent this (look at HandlerRoutine delegate in the example).

    You haven't showed how you are interacting with the native code, but most likely so long as the delegate is not collected, it will be fine as per Microsoft's example.  I don't remember ever seeing an issue involving native code and the movement of delegate or the actual JIT code for the method.

    Typically the delegate is passed as the type of the P/Invoke parameter, but you can get some idea of what is going on by also noticing that there is a http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getfunctionpointerfordelegate.aspx method.  Interestingly, even if you call this to get the function IntPtr, you must still keep the delegate alive as explained in the documentation.

     

     

    • Marked as answer by Cookie Luo Wednesday, December 22, 2010 4:51 AM
    Wednesday, December 15, 2010 11:49 PM

All replies

  • If the delegate itself gets collected, you've got a big problem.  You need to design your app so that this doesn't happen.  For example, see http://msdn.microsoft.com/en-us/library/system.gc.keepalive.aspx which shows one way to prevent this (look at HandlerRoutine delegate in the example).

    You haven't showed how you are interacting with the native code, but most likely so long as the delegate is not collected, it will be fine as per Microsoft's example.  I don't remember ever seeing an issue involving native code and the movement of delegate or the actual JIT code for the method.

    Typically the delegate is passed as the type of the P/Invoke parameter, but you can get some idea of what is going on by also noticing that there is a http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.getfunctionpointerfordelegate.aspx method.  Interestingly, even if you call this to get the function IntPtr, you must still keep the delegate alive as explained in the documentation.

     

     

    • Marked as answer by Cookie Luo Wednesday, December 22, 2010 4:51 AM
    Wednesday, December 15, 2010 11:49 PM
  • ok here is the code

     

     

    native code:

     

    typedef void (__stdcall *MailNotificationHandler)(struct_MailInformation *Info);

    MailNotificationHandler nativeDelegate;

    void MailDispatcher()

    {

                          while(true)

    {

                                  waitfornewMail();

                   struct_MailInformation ob;

                                      nativeDelegate(&ob);

    }

    }

     

    extern "C" __declspec(dllexport) void RegisterForMailNotification(MailNotificationHandler ManagedDelegate)

    {

      nativeDelegate = ManagedDelegate;

    }

     

     

    Manged Code

     

     

    class Main

    {

                       [DllImport("Test.dll")]

    public extern static void RegisterForMailNotification (IntPtr p);

    public delegate void MailRecevieHandler(ref struct_MailInformation ob);

    static MailRecevieHandler MyDelegate;

    static GCHandle myHandle;

     static IntPtr pPointer;

    public static void Main()

    {

     myDelegate = new MailRecevieHandler (OnMailReceived);

    myHandle = GCHandle.alloc(myDelegate);

     pPointer = Marshal.GetFunctionPointerForDelegate(myDelegate);

    RegisterForMailNotification (pPointer);

              WaitForInfiniteTime();

    }

     

     

    static void OnMailReceived(struct_MailInformation ob)

    {

             // New Mail Received

    }

    }

     

     

     

    My Question is What happens if manged Delegate Relocates the delegate object.  there is no way to know the native code

    about this . How the CLR Handles this 

    • Edited by drunkanmaster Thursday, December 16, 2010 2:23 AM more clarification
    Thursday, December 16, 2010 2:21 AM
  • Hi Drunkanmaster

    BinaryCoder's answer is quite comprehensive. You can follow.

    Best Regards,


    Cookie Luo[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, December 21, 2010 8:54 AM