-
Notifications
You must be signed in to change notification settings - Fork 135
UnityEngineTimePropertyGetDeltaTime's return type is wrong #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
|
Here is the stack trace: Stacks for Running Threads:` The Chinese words mean "Access Invalid Address". It crashed at the call of Plugin::UnityEngineTimePropertyGetDeltaTime() in UnityEngine::Time::GetDeltaTime, and in disassembly it just crased at the mov instruction which moves the return value(it happened to be a 0x0 pointer) to a register after the call instruction. According to what happend, I don't think that the C# delegate return type float is the same with the c++ struct System::Single when the delegate function is declared with MonoPInvokeCallback, although in c# float and System.Single are same. By the way, I work on Window 10 x64 system. I built the x64 native script all with VS2017 and I used Unity2018.3.12 to test it. After I changed the c++ Plugin::UnityEngineTimePropertyGetDeltaTime's return type to float, everything is ok now. |
I tried to reproduce this using the same environment: Windows 10 64-bit, VS 2017 build, and Unity 2018.3.12. Unfortunately, it didn't crash for me. I'm using a clean checkout of the repository with no modifications to the example Unity project. Do you have some other changes that made it happen? Is the issue only intermittently occurring? |
It's so weird. With the clean checkout, I only moved the built NativeScript.dll from Assets\Plugins\ to Assets\Plugins\Editor without any codes changed, or it would report dll missing. My Win10 OS version is 1803, not sure if it's related. Thanks any way, and feel free to close this issue if everything is fine with you:). |
Hi again! |
Thanks for the link; it was very informative! I see how the scenario described is similar to this one and how removing the constructors would fix the crash you're seeing. Unfortunately, I still can't reproduce it despite making several builds and running the example included in the repo several time. I'm reluctant to remove a useful feature (the constructors) until I can do that, so any tips you can give on how I can reproduce would still be very nice to have. In the meantime, I'm glad you have a workaround for the issue. |
Hello there, I'm pretty sure this is the same bug as #11. Which means returning the value through a helper function with an As for a solution... I manually modified the bindings in order to pass deltaTime's value from C# to C++ through a parameter/pointer instead of returning it (without relying on a custom helper method), and it seems this does the trick so far. It would probably be safer for the bindings generator to always use this instead of This has been bugging me for a while... Now that we have a clear explanation I might try to implement this into the generator when I have some time. Not sure when that happens, but if you're interested I can make a pull request for it afterwards. |
@Jrius, feel free to submit this as a pull request! :) |
Hi Jackson, I'm hitting this same issue, Visual Studio 2019, 64 bit, windows 8. If I create this simple class on the C# side: The generator converts it to this: And it crashes when used. If I manually change it to this (in all the right places): It works fine. I know I don't understand all the cases you're covering in the code, but shouldn't the base signature in CPP always use native types (like int32_t) as opposed to the wrappers (like System::Int32)? For both arguments and return values? |
The crash you're seeing, however, is due to System::Int32 specifying a constructor. This tips the (overzealous) compiler into thinking, "oh, it has a constructor, so always returning a copy must be expensive. Let's optimize this call by moving the memory instead of copying it !" (this is called Return-Value Optimization, if you don't use C++ regularly you may not have heard of it). Unfortunately, for some reason C++ does not expose any way to selectively disable the overzealous RVO for some methods. As for C#, it simply cannot guess whether RVO is used or not because RVO is implemented differently across compilers. This is why this bug happens much more rarely on Mac. In my experience, this issue got worse recently due to either the .NET 4 runtime, or an upgrade to the VS compiler. The workaround is to never use the I have a modified version of the project that automatically returns by pointer arg, and so far it seems to be working really well on a fairly big project ! But it's based off a custom and outdated version of the scripts, so I'll have to do a bit of work to upgrade it to the current version. |
…r than struct types (e.g. int32_t instead of System::Int32).
I picked up the fix and it fixes my issue. Thanks Jackson! |
Hi!
When the NativeScript.dll is used, Unity always crashes because the C++ UnityEngineTimePropertyGetDeltaTime function pointer's return type is System::Single which is diffrent from the return type of the C# delegate UnityEngineTimePropertyGetDeltaTimeDelegateType. UnityEngineTimePropertyGetDeltaTime should return float instead of System::Single.
The text was updated successfully, but these errors were encountered: