jacksondunstan / UnityNativeScripting

Unity Scripting in C++
https://jacksondunstan.com/articles/3938
MIT License
1.32k stars 135 forks source link

[Critical] .Net4.x crashes #40

Open sekkit opened 4 years ago

sekkit commented 4 years ago

Unity2018.4.5f1, MacOS works fine, but windows crashes.

issue reproduce: When calling MyGameAbstractBaseBallScriptUpdate

CSharp code:

                public override void Update()
        {
            if (CppHandle != 0)
            {
                int thisHandle = CppHandle;
                NativeScript.Bindings.MyGameAbstractBaseBallScriptUpdate(thisHandle);
                if (NativeScript.Bindings.UnhandledCppException != null)
                {
                    Exception ex = NativeScript.Bindings.UnhandledCppException;
                    NativeScript.Bindings.UnhandledCppException = null;
                    throw ex;
                }
            }
        }

When executes to the line: auto returnValue = Plugin::UnityEngineTimePropertyGetDeltaTime(); It crashes. C++ code:

System::Single UnityEngine::Time::GetDeltaTime()
    {
        auto returnValue = Plugin::UnityEngineTimePropertyGetDeltaTime();
        if (Plugin::unhandledCsharpException)
        {
            System::Exception* ex = Plugin::unhandledCsharpException;
            Plugin::unhandledCsharpException = nullptr;
            ex->ThrowReferenceToThis();
            delete ex;
        }
        return returnValue;
    }

Exception thrown at 0x00007FFDE974D5D9 (NativeScript_temp.dll) in Unity.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

Stacktrace:


========== OUTPUTTING STACK TRACE ==================

0x00007FFE2AEBD5D9 (NativeScript_temp) [c:\projects\research\unitycpp\unity\libs\cppsource\nativescript\bindings.cpp:5498] UnityEngine::Time::GetDeltaTime 
0x00007FFE2AEA3B9A (NativeScript_temp) [c:\projects\research\unitycpp\unity\libs\cppsource\game\game.cpp:38] MyGame::BallScript::Update 
0x00007FFE2AEBFCA7 (NativeScript_temp) [c:\projects\research\unitycpp\unity\libs\cppsource\nativescript\bindings.cpp:5819] MyGameAbstractBaseBallScriptUpdate 
0x000000004E2DC2C5 (Mono JIT Code) (wrapper managed-to-native) object:wrapper_native_00007FFE2AEA12AD (int)
0x000000004E367898 (Mono JIT Code) [C:\Projects\Research\unitycpp\Unity\Assets\NativeScript\Bindings.cs:2121] MyGame.BaseBallScript:Update () 
0x000000004EE32FC8 (Mono JIT Code) (wrapper runtime-invoke) object:runtime_invoke_void__this__ (object,intptr,intptr,intptr)
0x00007FFDBABCB970 (mono-2.0-bdwgc) [c:\users\bokken\builds\vm\mono\mono\mini\mini-runtime.c:2809] mono_jit_runtime_invoke 
0x00007FFDBAB51922 (mono-2.0-bdwgc) [c:\users\bokken\builds\vm\mono\mono\metadata\object.c:2919] do_runtime_invoke 
0x00007FFDBAB5A91F (mono-2.0-bdwgc) [c:\users\bokken\builds\vm\mono\mono\metadata\object.c:2966] mono_runtime_invoke 
0x0000000140C03DAA (Unity) scripting_method_invoke
0x0000000140BF400A (Unity) ScriptingInvocation::Invoke
0x0000000140BBCB57 (Unity) MonoBehaviour::CallMethodIfAvailable
0x0000000140BBD271 (Unity) MonoBehaviour::CallUpdateMethod
0x00000001406EEBDC (Unity) BaseBehaviourManager::CommonUpdate<BehaviourManager>
0x00000001406F5266 (Unity) BehaviourManager::Update
0x0000000140960373 (Unity) `InitPlayerLoopCallbacks'::`2'::UpdateScriptRunBehaviourUpdateRegistrator::Forward
0x000000014095EFC7 (Unity) ExecutePlayerLoop
0x000000014095F093 (Unity) ExecutePlayerLoop
0x0000000140962351 (Unity) PlayerLoop
0x0000000141349FDF (Unity) PlayerLoopController::UpdateScene
0x0000000141339383 (Unity) PlayerLoopController::EnterPlayMode
0x0000000141345323 (Unity) PlayerLoopController::SetIsPlaying
0x0000000141348282 (Unity) Application::TickTimer
0x00000001414A4C53 (Unity) MainMessageLoop
0x00000001414A693D (Unity) WinMain
0x000000014249BABA (Unity) __scrt_common_main_seh
0x00007FFE3E237BD4 (KERNEL32) BaseThreadInitThunk
0x00007FFE3EBCCE71 (ntdll) RtlUserThreadStart

Game booted up
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
NativeScript.Bindings:UnityEngineDebugMethodLogSystemObject(Int32) (at Assets\NativeScript\Bindings.cs:1372)
System.Object:wrapper_native_00007FFE2AEA18DE(IntPtr, Int32, InitMode)
NativeScript.Bindings:OpenPlugin(InitMode) (at Assets\NativeScript\Bindings.cs:836)
NativeScript.Bindings:Open(Int32) (at Assets\NativeScript\Bindings.cs:622)
NativeScript.BootScript:Awake() (at Assets\NativeScript\BootScript.cs:36)

(Filename: Assets/NativeScript/Bindings.cs Line: 1372)

Crash!!!
jacksondunstan commented 4 years ago

It's working fine for me on Windows. Can you please provide more information such as your version of Windows, your C++ compiler version, the reproduction rate of the crash, and any modifications you've made since cloning the repo?

sekkit commented 4 years ago

It's working fine for me on Windows. Can you please provide more information such as your version of Windows, your C++ compiler version, the reproduction rate of the crash, and any modifications you've made since cloning the repo?

Windows 10 1903 latest 64bit Unity2018.4.5f1 VS2015/VS2017/VS2019

switching to .net4.x, it crashes everytime^^ Tested on your original project, no code changed at all.

jacksondunstan commented 4 years ago

I am also seeing the issue in 2018.3.12f1 after switching to .NET 4.x and running in the editor. I'll investigate a solution. In the meantime, please use .NET 3.5.

sekkit commented 3 years ago

Has the issue been solved?

jacksondunstan commented 3 years ago

There have been several bug fixes since the issue was reported, so it's possible it's fixed. Can you reproduce with the latest version?

sekkit commented 3 years ago

ok. btw I hope coroutine feature could be implemented. either by upgrading c++ version or the other. I've got a plan for impl automatic types generation feature with Mono.Cecil (including nested types). which would help with developing larger projects.

jacksondunstan commented 3 years ago

Sounds cool. Definitely feel free to submit a PR if you implement such a feature.