c# - Do I need to pin an anonymous delegate? -


I'm calling CopyFileEx from a C # application, to get a notification on file with an unknown representative The copy progress is being passed in the LPPROGRESS_ROUTINE parameter.

My question is, the anonymous representative needs to be pinned and why (or why not).

In addition, the answer varies if:

  1. CopyFileEx was not blocked.
  2. If I went to a representative who was not anonymous.

    Thank you!

    The representative does not need to pin a managed object Pinned If this garbage collector can not be transferred If the marshaling information is correct then the marshalings layer will ensure that an indicator passes to stabilize something.

    However, the above comment, where you suggest that the local variable can keep alive variable indicates a misunderstanding of the lifetime. I mention your imagery, which states:

    The real-life implementation of the local variable is dependent-dependent. For example, a compiler can statically determine that a local variable is used only for a small part of that block in the block. Using this analysis, the compiler can generate that code, which results in the storage of the variable which has a shorter life span than its block. The storage referenced by the local reference variable has been retrieved independently of the lifetime of the local reference variable

    , if you say:

      () {Fu Fu = Gatefu (); UnmanagedLibrary.DoSomethingToFoo (foo); }   

    Then you know Jitter ", I know that any managed code allows Fu to use the moment after it is not reused, so unnecessary calls Can be recovered instantly, on that object from another thread at that time ". Which means that unmanaged calls can work on an object when it is suddenly dealt on another thread.

    This is especially dirty if Fu is a destroyer The final form code will be run on the other thread, while the object is in use of unmanaged library, and the heaven only knows what kind of disaster will be.

    In this situation, you need to use KeepAlive to keep the managed object alive. Do not depend on local variables;

    See for more information. Local variables have been specially marked as not .

Comments