xLua icon indicating copy to clipboard operation
xLua copied to clipboard

typeHasEditorRef 可能触发 StackOverflowException

Open oOtroyOo opened this issue 7 years ago • 4 comments

我在配置文件中使用了ExampleConfig.cs 中的示范的typeHasEditorRef 和 delegateHasEditorRef 例如在Gen的时候,进行到一个方法

public abstract class InstanceMonoBehavior<T> : MonoBehaviour where T : InstanceMonoBehavior<T>
    {
        private static T _instance;


        public static T GetInstance()
        {
            if (_instance == null)
            {
 ................

其中T GetInstance()就会触发 System.StackOverflowException: The requested operation caused a stack overflow

不仅这个方法,LitJson也有几个方法会出现报错。

我自己改了typeHasEditorRef能解决,可否在ExampleConfig.cs中在处理一下呢?

oOtroyOo avatar Sep 10 '19 10:09 oOtroyOo

你可以把你的修改提个PR过来啊,开源本来就是要一起改进

chexiongsheng avatar Sep 11 '19 01:09 chexiongsheng

    static bool typeHasEditorRef(Type type)
    {
        if (type.Namespace != null && (type.Namespace == "UnityEditor" || type.Namespace.StartsWith("UnityEditor.")))
        {
            return true;
        }
        if (type.IsNested)
        {
            return type.DeclaringType != type && type.BaseType != type.DeclaringType && typeHasEditorRef(type.BaseType);
        }
        if (type.IsByRef || type.IsArray)
        {
            return typeHasEditorRef(type.GetElementType());
        }
        if (type.IsGenericType)
        {
            foreach (var typeArg in type.GetGenericArguments())
            {
                if (typeHasEditorRef(typeArg))
                {
                    return true;
                }
            }
        }
        return false;
    }

这是我修改的 type.IsNested 下,加了两个排除相等

以及由typeHasEditorRef(type.DeclaringType) 更改为了typeHasEditorRef(type.BaseType);

尚未确认这一行修改是否得当,DeclaringType 和BaseType意义都不同,不能擅自提交PR

oOtroyOo avatar Sep 11 '19 02:09 oOtroyOo

这个是不是和我这个问题是同一个啊。 https://github.com/Tencent/xLua/issues/631

wumohai avatar Sep 19 '19 09:09 wumohai

改成 type.BaseType 还是一样, 我这里要么就是 Unity 崩溃了, 要么就还是 stack overflow, 不过我改成下面这个貌似可行, 没报错了起码


    static bool typeHasEditorRef(Type type)
    {
        if(type.Namespace != null &&
            (type.Namespace == "UnityEditor" || type.Namespace.StartsWith("UnityEditor."))) {
            return true;
        }
        if(type.IsNested) {
            return typeHasEditorRef(type.DeclaringType);
        }
        if(type.IsByRef || type.IsArray) {
            return typeHasEditorRef(type.GetElementType());
        }
        if(type.IsGenericType) {
            foreach(var typeArg in type.GetGenericArguments()) {
                if(typeArg.IsNested) {
                    continue;
                }
                if(typeHasEditorRef(typeArg)) {
                    return true;
                }
            }
        }
        return false;
    }

我这都是 type.IsGenericType 又跑到 type.IsNested 里面去了好像, 在 type.IsGenericType 里面过滤一下至少能解决我的问题 因为我每次都要google来找这个issue, 所以顺便在这里记录一下, 免得下次我又忘了怎么改, 我记得我以前就改成功过的, 不过过了好久结果又忘掉了, 只记得有这个issue里有提到.

PleijaNet avatar Dec 04 '20 01:12 PleijaNet