-
Notifications
You must be signed in to change notification settings - Fork 133
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
Crashing on some methods #11
Comments
Hi @Jrius, I just tried this out using macOS in both Editor and Standalone and didn't run into any issues. Is this issue only happening for you on Windows? |
I don't have a Mac to test with unfortunately... But I'm confident this is a Windows-only bug. Today I tried creating various similar methods in my C# game scripts, and found that methods or property getters crash when returning most value types. Wow, crazy I didn't notice sooner - but then my project is mostly about feeding data to Unity, and I rarely need to access some of the info I just gave it. |
@Jrius, thanks for continuing to investigate this with me. I spent some time looking into the issue this weekend, including reproducing it on Windows 10 64-bit using your example code. I made a small example project to strip away as much of UnityNativeScripting as possible. I ended up with a single C++ file compiled into a struct TestStruct
{
float f01; // 4 bytes - OK
float f02; // 8 bytes - OK
float f03; // 12 bytes - OK
float f04; // 16 bytes - OK
float f05; // 20 bytes - OK
float f06; // 24 bytes - OK
float f07; // 28 bytes - OK
float f08; // 32 bytes - OK
float f09; // 36 bytes - OK
float f10; // 40 bytes - OK
float f11; // 44 bytes - Crash
};
extern "C" __declspec(dllexport) void CppFunc(TestStruct(*fp)())
{
TestStruct s = fp();
} Then I made a Unity project that's empty except for one C# file: using AOT;
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public struct TestStruct
{
public float f01; // 4 bytes - OK
public float f02; // 8 bytes - OK
public float f03; // 12 bytes - OK
public float f04; // 16 bytes - OK
public float f05; // 20 bytes - OK
public float f06; // 24 bytes - OK
public float f07; // 28 bytes - OK
public float f08; // 32 bytes - OK
public float f09; // 36 bytes - OK
public float f10; // 40 bytes - OK
public float f11; // 44 bytes - Crash
}
public delegate TestStruct DelegateType();
public class TestScript : MonoBehaviour
{
[MonoPInvokeCallback(typeof(DelegateType))]
public static TestStruct ReturnTestStruct()
{
return default(TestStruct);
}
[DllImport("CppLib")]
static extern void CppFunc(IntPtr fp);
void Start()
{
DelegateType del = new DelegateType(ReturnTestStruct);
IntPtr fp = Marshal.GetFunctionPointerForDelegate(del);
CppFunc(fp);
}
} Here's what this does:
This produces the same null dereference crash that you reported and I reproduced in UnityNativeScripting. The problem notably doesn't happen if any of the following are true:
So this seems like an edge case that should only occur with large structs like public static class Workaround
{
public static void TransformGetLocalToWorldMatrix(Transform t, ref Matrix4x4 m)
{
m = t.localToWorldMatrix;
}
} Then you'd expose it in the JSON config like this: {
"Types": [
{
"Name": "Workaround",
"Methods": [
{
"Name": "TransformGetLocalToWorldMatrix",
"ParamTypes": [
"UnityEngine.Transform",
"UnityEngine.Matrix4x4"
]
}
]
}
]
} Then you'd call it from C++ like this: void Foo()
{
GameObject go("test go");
Transform t(go.GetTransform());
Matrix4x4 m;
Workaround::TransformGetLocalToWorldMatrix(t, &m);
} Hopefully that unblocks you until I can commit a proper solution. |
Hey. Sorry for not answering sooner, I had one of those crazy week at work... |
Hello again :)
This time I'm having trouble with some functions crashing Unity, and having a hard time nailing down the cause... I was hoping you could help me figure it out.
This is the code I use (can be pasted directly into the sample .cpp file):
Oh, and the relevant NativeScriptTypes.json, if that helps:
I attached a debugger to the Unity Editor, and it doesn't say much (Access violation reading location 0x0, when calling
Plugin::UnityEngineTransformPropertyGetLocalToWorldMatrix(Handle);
in the generated bindings). Happens on both Unity 2017 and 2018, on both Debug and Release build, on Windows.Those methods are pretty straightforward. GetPosition and GetLocalToWorldMatrix are almost identical when decompiling UnityEngine.dll - they are just calls to native code. I thought it might be because it's returning a Matrix4x4, but
LayerMask::NameToLayer
crashes too and it's only returning an int...Out of curiosity, I tried moving those calls to a C# script, then call the C# script from the C++ code - the result was the same.
The text was updated successfully, but these errors were encountered: