[marshal] Free delegates with target that are passed to native code. (mono/mono#15935)
authorVlad Brezae <brezaevlad@gmail.com>
Thu, 1 Aug 2019 13:06:05 +0000 (16:06 +0300)
committerGitHub <noreply@github.com>
Thu, 1 Aug 2019 13:06:05 +0000 (16:06 +0300)
commit1a6ffad8b8bf1ebfe696384e246de6466c68a3bd
tree7aa09d505b03ef2d1af04ed5481a99137cbb45cd
parent15bcac530533bf1bd7ef11ab69103896e468e9c1
[marshal] Free delegates with target that are passed to native code. (mono/mono#15935)

* [marshal] Always use gchandles in delegate_hash_table

Makes the code easier to follow and it also fixes race from https://github.com/mono/mono/commit/mono/mono@caa4a753ca8e15d43baaa01adb0f56f374b74a2b with boehm.

* [marshal] Free delegates with target that are passed to native code.

For static method delegates, we have a unique delegate_trampoline that is shared among all delegates. We always keep alive the first static method delegate passed to native, by creating a normal gchandle to it and storing it in delegate_hash_table. For instance methods, each delegate will create a separate wrapper and these wrappers are never shared (which was wrongly assumed in https://github.com/mono/mono/commit/mono/mono@caa4a753ca8e15d43baaa01adb0f56f374b74a2b). We shuldn't keep the delegate alive and this commit reverts the behavior for delegate with instance methods introduced in the mentioned commit.

Fixes https://github.com/mono/mono/issues/15751

Commit migrated from https://github.com/mono/mono/commit/e3ca891235609663785d70aa1bbc544232070321
src/mono/mono/metadata/marshal.c