[marshal] Free delegates with target that are passed to native code. (#15935)
commite3ca891235609663785d70aa1bbc544232070321
authorVlad Brezae <brezaevlad@gmail.com>
Thu, 1 Aug 2019 13:06:05 +0000 (1 16:06 +0300)
committerGitHub <noreply@github.com>
Thu, 1 Aug 2019 13:06:05 +0000 (1 16:06 +0300)
treec65b35dc122d49c451152b25a2f4503470d66d7b
parentce398411a06b85441611565b9b6119c2702cb284
[marshal] Free delegates with target that are passed to native code. (#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/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/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
mono/metadata/marshal.c