Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are delegates always pinned?

A coworker of mine made the claim that delegates are always pinned in the heap in .net. I can't seem to find any authoritative source on whether this information is true.

Is it true?

like image 297
recursive Avatar asked Nov 21 '25 11:11

recursive


1 Answers

This is simply not true. Delegates are not pinned in .Net.

Delegates act as if they are pinned when they are used as a parameter to a PInvoke function. The CLR allocates a native thunk which holds a reference to the delegate. But this reference does not pin the delegate nor does it act as a strong reference to the object.

http://msdn.microsoft.com/en-us/magazine/cc164193.aspx#S7

Do note that pinning an object is not free. It actually incurs a fairly significant cost to the GC. It is fine in small doses but having every .Net delegate be pinned would likely have a very noticeable impact on performance

like image 173
JaredPar Avatar answered Nov 24 '25 03:11

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!