Код изменен для удаления пространств имен и префиксов, не имеющих отношения к вопросу
Код: Выделить всё
public static class Engine
{
// Causing issues with sometimes throwing access violation
[DllImport("engine", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern UInt32 EventScheduler_ScheduleLocalEvent(
int priority, UInt64 delay, delegate* unmanaged[Cdecl] callback, nint obj);
// Works fine every time, but is much slower seems due to delegate instatiations
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void EventScheduler_ScheduleLocalEvent_callbackDelegate(nint obj);
[DllImport("engine", CallingConvention = CallingConvention.Cdecl)]
public static unsafe extern UInt32 EventScheduler_ScheduleLocalEvent(
int priority,
UInt64 delay,
[MarshalAs(UnmanagedType.FunctionPtr)] EventScheduler_ScheduleLocalEvent_callbackDelegate callback,
nint obj);
}
public static class EventScheduler
{
public delegate void EventCallback();
// attribute is only present in the function pointer senario
[UnmanagedCallersOnly(CallConvs = new[] { typeof(CallConvCdecl ) }, EntryPoint = "ScheduleLocalEvent_EventCallbackFunction")]
private static void ScheduleLocalEvent_EventCallbackFunction(nint handlePtr)
{
Thread.BeginThreadAffinity();
GCHandle handle = GCHandle.FromIntPtr(handlePtr);
var cb = (EventScheduler.EventCallback)handle.Target!;
cb();
handle.Free();
Thread.EndThreadAffinity();
}
public static UInt32 ScheduleLocalEvent(int priority, SimulationTime delayTime, EventCallback eventCallback)
{
GCHandle handle = GCHandle.Alloc(eventCallback, GCHandleType.Normal);
IntPtr handlePtr = GCHandle.ToIntPtr(handle);
unsafe
{
return Engine.EventScheduler_ScheduleLocalEvent(
priority, delayTime, &ScheduleLocalEvent_EventCallbackFunction, handlePtr);
}
}
Код: Выделить всё
#define API extern "C" __declspec(dllexport)
API uint32_t EventScheduler_ScheduleLocalEvent(int priority, uint64_t delay, void(__cdecl* callback)(void* obj), void* obj)
{
ERSAssert(Ers::Core::InsideSubModel());
Core::SyncManagerBase& syncManager = Ers::Core::GetSyncManagerBase();
return syncManager.ScheduleEvent(priority, delay, [callback, obj]() { callback(obj); });
}
AWealthOfRows.exe (процесс 39016) завершается с кодом -1073741819.
Со следующим выводом
Код: Выделить всё
`' AWealthOfRows.exe' (Win32): Loaded
'C:\Projects\Testing\Enterprise_Resource_Simulator\bindings\CSharp\AWealthOfRows\bin\Debug\net8.0\AWealthOfRows.exe'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\kernel32.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\KernelBase.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\user32.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\win32u.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\gdi32.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\gdi32full.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\msvcp_win.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\ucrtbase.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\shell32.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\advapi32.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\msvcrt.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\sechost.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\bcrypt.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\rpcrt4.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\imm32.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Program
Files\dotnet\host\fxr\9.0.0-preview.3.24172.9\hostfxr.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\hostpolicy.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\coreclr.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Windows\System32\ole32.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Windows\System32\combase.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\oleaut32.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\bcryptprimitives.dll'. The thread 41728
has exited with code 0 (0x0). 'AWealthOfRows.exe' (Win32): Loaded
'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Private.CoreLib.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\clrjit.dll'.
'AWealthOfRows.exe' (CoreCLR: DefaultDomain): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Private.CoreLib.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\kernel.appcore.dll'. 'AWealthOfRows.exe'
(Win32): Loaded
'C:\Projects\Testing\Enterprise_Resource_Simulator\bindings\CSharp\AWealthOfRows\bin\Debug\net8.0\AWealthOfRows.dll'.
The thread 41464 has exited with code 0 (0x0). 'AWealthOfRows.exe'
(CoreCLR: clrhost): Loaded
'C:\Projects\Testing\Enterprise_Resource_Simulator\bindings\CSharp\AWealthOfRows\bin\Debug\net8.0\AWealthOfRows.dll'.
Symbols loaded. 'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Runtime.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Runtime.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Windows\System32\icu.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program Files\Microsoft Visual
Studio\2022\Community\Common7\IDE\CommonExtensions\Microsoft\HotReload\Microsoft.Extensions.DotNetDeltaApplier.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'c:\program
files\microsoft visual
studio\2022\community\common7\ide\commonextensions\microsoft\hotreload\Microsoft.Extensions.DotNetDeltaApplier.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.IO.Pipes.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.IO.Pipes.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Linq.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Linq.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Collections.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Collections.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Console.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Console.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Threading.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Threading.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Runtime.InteropServices.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Runtime.InteropServices.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Threading.Overlapped.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Threading.Overlapped.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Security.AccessControl.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Security.AccessControl.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Security.Principal.Windows.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Security.Principal.Windows.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Windows\System32\ntmarta.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Security.Claims.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Security.Claims.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Runtime.Loader.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Runtime.Loader.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Projects\Testing\Enterprise_Resource_Simulator\bindings\CSharp\AWealthOfRows\bin\Debug\net8.0\Ers.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Collections.Concurrent.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded
'C:\Projects\Testing\Enterprise_Resource_Simulator\bindings\CSharp\AWealthOfRows\bin\Debug\net8.0\Ers.dll'.
Symbols loaded. 'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded
'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Collections.Concurrent.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Projects\Testing\Enterprise_Resource_Simulator\bindings\CSharp\AWealthOfRows\bin\Debug\net8.0\ers-engine.dll'.
Symbols loaded. 'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\setupapi.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\winmm.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\version.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\vcruntime140d.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\msvcp140d.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Windows\System32\vcruntime140_1d.dll'. 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Windows\System32\ucrtbased.dll'.
'AWealthOfRows.exe' (Win32): Loaded
'C:\Windows\System32\cfgmgr32.dll'. 'AWealthOfRows.exe' (Win32):
Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Diagnostics.Process.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Diagnostics.Process.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.ComponentModel.Primitives.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.ComponentModel.Primitives.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Memory.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Memory.dll'.
'AWealthOfRows.exe' (Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Text.Encoding.Extensions.dll'.
The thread 20540 has exited with code 0 (0x0). The thread 23312 has
exited with code 0 (0x0). 'AWealthOfRows.exe' (CoreCLR: clrhost):
Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Text.Encoding.Extensions.dll'.
The thread 9224 has exited with code 0 (0x0). 'AWealthOfRows.exe'
(Win32): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Threading.Thread.dll'.
'AWealthOfRows.exe' (CoreCLR: clrhost): Loaded 'C:\Program
Files\dotnet\shared\Microsoft.NETCore.App\8.0.5\System.Threading.Thread.dll'.
Exception thrown at 0x000001F303570860 in AWealthOfRows.exe:
0xC0000005: Access violation reading location 0x000001F30385FB32. The
Common Language Runtime cannot stop at this exception. Common causes
include: incorrect COM interop marshalling and memory corruption. To
investigate further use native-only debugging. Unhandled exception at
0x000001F303570860 in AWealthOfRows.exe: 0xC0000005: Access violation
reading location 0x000001F30385FB32. The Common Language Runtime
cannot stop at this exception. Common causes include: incorrect COM
interop marshalling and memory corruption. To investigate further use
native-only debugging. The thread '.NET Counter Poller' (35516) has
exited with code 3221225477 (0xc0000005). The thread '.NET TP Gate'
(24688) has exited with code 3221225477 (0xc0000005). The thread 33416
has exited with code 3221225477 (0xc0000005). The thread '.NET
Finalizer' (38504) has exited with code 3221225477 (0xc0000005). The
thread '.NET EventPipe' (23352) has exited with code 3221225477
(0xc0000005). The thread 36944 has exited with code 3221225477
(0xc0000005). The thread 30968 has exited with code 3221225477
(0xc0000005). The thread 3560 has exited with code 3221225477
(0xc0000005). The thread 41012 has exited with code 3221225477
(0xc0000005). The thread 13568 has exited with code 3221225477
(0xc0000005). The thread 22992 has exited with code 3221225477
(0xc0000005). The thread '.NET TP Worker' (38436) has exited with code
3221225477 (0xc0000005). The thread 41284 has exited with code
3221225477 (0xc0000005). The thread 40972 has exited with code
3221225477 (0xc0000005). The thread 40980 has exited with code
3221225477 (0xc0000005). The thread 41972 has exited with code
3221225477 (0xc0000005). The thread 39600 has exited with code
3221225477 (0xc0000005). The thread 41096 has exited with code
3221225477 (0xc0000005). The thread 41968 has exited with code
3221225477 (0xc0000005). The thread 7472 has exited with code
3221225477 (0xc0000005). The thread '.NET ThreadPool IO' (36056) has
exited with code 3221225477 (0xc0000005). The thread 16184 has exited
with code 3221225477 (0xc0000005). The thread 11736 has exited with
code 3221225477 (0xc0000005). The thread '.NET TP Worker' (33156) has
exited with code 3221225477 (0xc0000005). The thread '.NET TP Worker'
(5368) has exited with code 3221225477 (0xc0000005). The thread '.NET
ThreadPool IO' (22304) has exited with code 3221225477 (0xc0000005).
The thread '.NET TP Worker' (42112) has exited with code 3221225477
(0xc0000005). The thread 40516 has exited with code 3221225477
(0xc0000005). The thread '.NET Tiered Compilation Worker' (2424) has
exited with code 3221225477 (0xc0000005). The program '[39016]
AWealthOfRows.exe' has exited with code 3221225477 (0xc0000005)
'Access violation'. `
Ради отладки я попробовал переключиться соглашение о вызовах от cdecl до stdcall, чтобы увидеть, связано ли это с повреждением стека.
Мои предположения таковы:
- статические функции, отмеченные [UnmanagedCallersOnly], имеют статический адрес, особенно если определена точка входа.
- Thread.BeginThreadAffinity(), за которым следует Thread.EndThreadAffinity () гарантирует, что неуправляемый поток сможет успешно начать выполнение управляемого кода.
- Функция вызывается потоком, не созданным на C#, существует множество потоков, вызывающих эту функцию
Подробнее здесь: https://stackoverflow.com/questions/785 ... ing-from-c