(PS:題名制限短っ!)
C# で SetActiveStateChangeCallBack を使いたいので、このように試しました:
1.プロジェクトの allow unsafe code と DX_USE_UNSAFE を設定しました
2.コード(部分)
----------------------------------------------------------------------------------------------------
using System;
using DxLibDLL;
namespace DXLib_TEST
{
class Program
{
public static bool IsWindowActive = true;
private unsafe static int ActiveStateChange(int ActiveState, void* UserData)
{
IsWindowActive = ActiveState == DX.TRUE;
return 0;
}
static void Main(string[] args)
{
DX.SetAlwaysRunFlag(DX.TRUE);
if (DX.DxLib_Init() != 0) {
return;
}
unsafe {
DX.SetActiveStateChangeCallBackFunction(ActiveStateChange, null);
}
// Main Loop
while (DX.ProcessMessage() == 0 && DX.ClearDrawScreen() == 0) {
// ここで IsWindowActive を使う
}
DX.DxLib_End();
return;
}
}
}
----------------------------------------------------------------------------------------------------
最初は無事動くけど、暫く経ったらウインドウ切り替える時にエラーが出る:
Managed Debugging Assistant 'CallbackOnCollectedDelegate'
Message=Managed Debugging Assistant 'CallbackOnCollectedDelegate' : 'A callback was made on a garbage
collected delegate of type 'DXLib_TEST!DxLibDLL.DX+SetActiveStateChangeCallBackFunctionCallback::Invoke'.
This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code,
they must be kept alive by the managed application until it is guaranteed that they will never be called.
(_ _|||) 直す方法は分かりません、教えてください。
追申:このエラーは Debug ビルドには出ません、Release なら出ます。