LoginSignup
1
1

More than 5 years have passed since last update.

[C#]DelegateのFunctionPointerがいつまで有効か

Posted at

はじめに

C#からC++等で作成したdllにCallbackを渡したいとき、
Marshal.GetFunctionPointerForDelegateを使ってDelegateからFunctionPointerにしたものを渡せばいい。
このときのFunctionPointerがいつまで有効なのかを調べた。

結論

Delegateの寿命が来るまでは少なくとも有効だと思われる。
よって、Delegateへの参照をどこかに残しておくとよい。
(monoの場合はAOT.MonoPInvokeCallbackAttribute属性を使えばstatic関数を直接渡すことが可能)

参考

[C#] デリゲートをGCの対象から外す方法
NativePluginsにC#デリゲートを登録する

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1