Tencent / xLua

xLua is a lua programming solution for C# ( Unity, .Net, Mono) , it supports android, ios, windows, linux, osx, etc.
Other
9.44k stars 2.46k forks source link

Webgl平台GC报错ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds #741

Open cyinwind opened 4 years ago

cyinwind commented 4 years ago

猜测原因: webgl平台对线程支持不完善,具体的报错我贴到最后。 我的解决方案: 用LockFreeQueue refQueue 替换 QueuerefQueue 以下是LockFreeQueue的代码,供参考:

public class LockFreeQueue { internal class SingleLinkNode where U : T { public SingleLinkNode Next; public U Item; } static private bool CAS(ref V location, V comparand, V newValue) where V : class { return comparand == Interlocked.CompareExchange(ref location, newValue, comparand); }

    SingleLinkNode<T> head;
    SingleLinkNode<T> tail;
    int count;

    public int Count { get { return count; } }
    public bool IsEmpty { get { return count <= 0; } }

    public LockFreeQueue()
    {
        head = new SingleLinkNode<T>();
        tail = head;
        count = 0;
    }

    public void Enqueue(T item)
    {
        SingleLinkNode<T> oldTail = null;
        SingleLinkNode<T> oldTailNext;

        SingleLinkNode<T> newNode = new SingleLinkNode<T>();
        newNode.Item = item;

        bool newNodeAdded = false;
        while (!newNodeAdded)
        {
            oldTail = tail;
            oldTailNext = oldTail.Next;

            if (tail == oldTail)
            {
                if (oldTailNext == null)
                    newNodeAdded = CAS<SingleLinkNode<T>>(ref tail.Next, null, newNode);
                else
                    CAS<SingleLinkNode<T>>(ref tail, oldTail, oldTailNext);
            }
        }
        CAS<SingleLinkNode<T>>(ref tail, oldTail, newNode);
        Interlocked.Increment(ref count);
    }

    public bool TryDequeue(out T item)
    {
        item = default(T);
        SingleLinkNode<T> oldHead = null;

        bool haveAdvancedHead = false;
        while (!haveAdvancedHead)
        {
            oldHead = head;
            SingleLinkNode<T> oldTail = tail;
            SingleLinkNode<T> oldHeadNext = oldHead.Next;

            if (oldHead == head)
            {
                if (oldHead == oldTail)
                {
                    if (oldHeadNext == null)
                    {
                        return false;
                    }
                    CAS<SingleLinkNode<T>>(ref tail, oldTail, oldHeadNext);
                }
                else
                {
                    item = oldHeadNext.Item;
                    haveAdvancedHead = CAS<SingleLinkNode<T>>(ref head, oldHead, oldHeadNext);
                }
            }
        }
        Interlocked.Decrement(ref count);
        return true;
    }

    public T Dequeue()
    {
        T result;
        if (TryDequeue(out result)) return result;
        return default(T);
    }

    public void Clear()
    {
        while (Count > 0)
        {
            Dequeue();
        }
    }
}

浏览器报错信息如下:

ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds at System.Array.Copy (System.Array sourceArray, System.Int32 sourceIndex, System.Array destinationArray, System.Int32 destinationIndex, System.Int32 length) [0x00000] in <00000000000000000000000000000000>:0

(Filename: currently not available on il2cpp Line: -1)

c587e4f0-fc76-436f-ab17-553632bb414b:3448:11 _JS_Log_Dump blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:3448 Z19WebGLPrintfConsolev7LogTypePKcPi blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:1905505 ZL20InternalErrorConsolePKcz blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:1905120 Z40DebugStringToFilePostprocessedStacktraceRK21DebugStringToFileData blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:1903861 Z17DebugStringToFileRK21DebugStringToFileData blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:1896851 ZN9Scripting23LogExceptionFromManagedE21ScriptingExceptionPtriPKcb blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:1985992 Z44DebugLogHandler_CUSTOM_InternalLogExceptionP12Il2CppObjectS0 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:6370219 _DebugLogHandler_Internal_LogException_m8400B0D97B8D4A155A449BD28A32C68373A1A856 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:16542634 _DebugLogHandler_LogException_m816CF2DDA84DFC1D1715B24F9626BD623FF05416 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:16542711 ZN23InterfaceActionInvoker2IP12Il2CppObjectbE6InvokeEjP11Il2CppClassS1_S1_b blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:12865518 _Logger_LogException_m362D3434D3B275B0B98E434BFBFBF52C76BBC9C3 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:17079564 __ZN23InterfaceActionInvoker2IP12Il2CppObjectbE6InvokeEjP11Il2CppClassS1_S1_b blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:12865518 _Debug_LogException_mBAA6702C240E37B2A834AA74E4FDC15A3A5589A9 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:16498967 _U3CU3Ec_U3CRegisterUECatcherU3Eb0_0_m55F5FA1A6235A1DF6306D232C25ACE1883E494D0 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15744534 _CompletedFileHandler_Invoke_m2217B0C5E72FC1FF3C5DE61212957267140C0F3E blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:13261054 Z93RuntimeInvoker_TrueVoid_t22962CB4C05B1D89B55A6E1139F0E87A90987017_RuntimeObjectRuntimeObjectPFvvEPK10MethodInfoPvPS4 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20628374 dynCall_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21632590 dynCall_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:27490 invoke_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:17564 ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5PP15Il2CppException blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20150557 ZN6il2cpp2vm7Runtime14DelegateInvokeEP14Il2CppDelegatePPvPP15Il2CppException blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20311055 ZN6il2cpp2vm7Runtime30CallUnhandledExceptionDelegateEP12Il2CppDomainP14Il2CppDelegateP15Il2CppException blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20312572 ZN6il2cpp2vm7Runtime18UnhandledExceptionEP15Il2CppException blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20312495 ZN6il2cpp2gc16GarbageCollector12RunFinalizerEPvS2 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20415658 _GC_invoke_finalizers blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20351874 _GC_notify_or_invoke_finalizers blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20351655 _GC_generic_malloc blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352455 _GC_malloc_kind blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352421 _GC_malloc_atomic blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352629 ZN6il2cpp2vm6Object15AllocatePtrFreeEmP11Il2CppClass blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20148732 ZN6il2cpp2vm5Array11NewSpecificEP11Il2CppClassm blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20148551 _il2cpp_array_new_specific blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20040507 _Queue_1_SetCapacity_m684BB4DCB9916091317862123593401F1B0B1DE7_gshared blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:18764731 _Queue_1_Enqueue_mDA8875F4090C3897C94D7BE04994D9635A023774_gshared blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:18766128 Z57Queue_1_Enqueue_mDA8875F4090C3897C94D7BE04994D9635A023774P49Queue_1_t47AC3FDC6F0D2A7D4E3424A2885FD486190D70A250GCAction_tA93067DCEAA3E434EE8E81D18EB2A8145E523872PK10MethodInfo blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15320748 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636861 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28235 invoke_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19054 _LuaEnv_equeueGCAction_m4480FE056BF525CB25C9802E6B05C3DAA02B52CB blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15320546 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636861 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28235 invoke_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19054 _LuaBase_Dispose_m6164A99EC0743718FBD51A2D36D4454A95814647 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15319839 ZN18VirtActionInvoker1IP50MaterialtF7DB3BF0C24DEC2FE0CB51E5DF5053D5223C8598E6InvokeEjP12Il2CppObjectS1 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:12582389 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636861 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28235 invoke_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19054 _LuaBase_Finalize_m0AF757A4E2103318E279C556AFE58655242484FD blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15319228 Z65RuntimeInvoker_TrueVoidt22962CB4C05B1D89B55A6E1139F0E87A90987017PFvvEPK10MethodInfoPvPS4 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20628311 dynCall_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21632590 dynCall_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:27490 invoke_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:17564 ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20150557 ZN6il2cpp2gc16GarbageCollector12RunFinalizerEPvS2_ blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20415642 _GC_invoke_finalizers blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20351874 _GC_notify_or_invoke_finalizers blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20351655 _GC_generic_malloc blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352455 _GC_malloc_kind blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352421 _GC_malloc_atomic blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352629 ZN6il2cpp2vm6Object15AllocatePtrFreeEmP11Il2CppClass blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20148732 ZN6il2cpp2vm5Array11NewSpecificEP11Il2CppClassm blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20148551 _il2cpp_array_new_specific blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20040507 _Queue_1_SetCapacity_m684BB4DCB9916091317862123593401F1B0B1DE7_gshared blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:18764731 _Queue_1_Enqueue_mDA8875F4090C3897C94D7BE04994D9635A023774_gshared blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:18766128 Z57Queue_1_Enqueue_mDA8875F4090C3897C94D7BE04994D9635A023774P49Queue_1_t47AC3FDC6F0D2A7D4E3424A2885FD486190D70A250GCAction_tA93067DCEAA3E434EE8E81D18EB2A8145E523872PK10MethodInfo blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15320748 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636861 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28235 invoke_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19054 _LuaEnv_equeueGCAction_m4480FE056BF525CB25C9802E6B05C3DAA02B52CB blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15320546 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636861 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28235 invoke_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19054 _LuaBase_Dispose_m6164A99EC0743718FBD51A2D36D4454A95814647 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15319839 ZN18VirtActionInvoker1IP50MaterialtF7DB3BF0C24DEC2FE0CB51E5DF5053D5223C8598E6InvokeEjP12Il2CppObjectS1 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:12582389 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636861 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28235 invoke_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19054 _LuaBase_Finalize_m0AF757A4E2103318E279C556AFE58655242484FD blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15319228 Z65RuntimeInvoker_TrueVoidt22962CB4C05B1D89B55A6E1139F0E87A90987017PFvvEPK10MethodInfoPvPS4 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20628311 dynCall_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21632590 dynCall_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:27490 invoke_iiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:17564 ZN6il2cpp2vm7Runtime6InvokeEPK10MethodInfoPvPS5_PP15Il2CppException blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20150557 ZN6il2cpp2gc16GarbageCollector12RunFinalizerEPvS2_ blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20415642 _GC_invoke_finalizers blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20351874 _GC_notify_or_invoke_finalizers blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20351655 _GC_generic_malloc blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352455 _GC_malloc_kind blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352421 _GC_malloc_atomic blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20352629 ZN6il2cpp2vm6Object15AllocatePtrFreeEmP11Il2CppClass blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20148732 ZN6il2cpp2vm6String7NewSizeEi blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20150188 ZN6il2cpp2vm6String8NewUtf16EPKDsi blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20150125 dynCall_iii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21631798 dynCall_iii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:27355 invoke_iii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:17294 ZN6il2cpp2vm6String6NewLenEPKcj blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20206788 ZN6il2cpp6icalls8mscorlib6System7Runtime15InteropServices7Marshal83PtrToStringAnsi_mscorlib_System_String_mscorlib_System_IntPtr_mscorlib_System_Int32Eli blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20086670 _Marshal_PtrToStringAnsi_m201DAEF9CEA3A0F557400AB639C939EED6A45B5B blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:14930831 _Lua_lua_tostring_mEB7B3527651D711B38DD3B4C64D92E9203216552 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15322651 dynCall_iiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21632250 dynCall_iiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:27435 invoke_iiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:17454 _StaticLuaCallbacks_Print_m0CF2C621CD735996DF1EDDCDEB6A171C6E42D4CD blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:14069597 dynCall_iii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21631798 dynCall_iii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:27355 invoke_iii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:17294 _ReversePInvokeWrapper_StaticLuaCallbacks_Print_m0CF2C621CD735996DF1EDDCDEB6A171C6E42D4CD blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:14069149 __ZL20csharp_function_wrapP9lua_State blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21431259 _luaD_precall blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21108589 _luaV_execute blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21120800 ZL6f_callP9lua_StatePv blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21170722 dynCall_vii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636298 dynCall_vii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28135 invoke_vii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:18854 _luaD_pcall blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21150513 _lua_pcall blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21405567 _Lua_lua_pcall_m97E7901117F39F04F75968DD7108B9550C46FE3D blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:15322452 _DelegateBridge_PCall_mA03E26CAF1228ACA1091839DDB065D9E28C7D3D5 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21074813 dynCall_viiiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21637875 dynCall_viiiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28395 invoke_viiiiii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19374 _DelegateBridge_Gen_Delegate_Imp9_m759BEA6495C6F009E332C1AD0D859559BFA88313 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21078901 _DeserializationEventHandler_Invoke_mE08F6E81D9791C6A69EA7BFCF50FA33B237B35D3 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:13469886 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636861 dynCall_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28235 invoke_viii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:19054 _LuaWebsock_U3C_ctorU3Eb12_0_m13E0E8609C65069BA2CCA6A2C664E556D08E240A blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:20934740 _ThreadStart_Invoke_m11B6A66E82F02C74399A7314C14C7F52393CC4B4 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:13796822 _WebSocket_DelegateOnOpenEvent_m9C6342AA07D1461D403BA1C32538B1062B945962 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:17189401 _WebSocketFactory_DelegateOnOpenEvent_mB0CF6AB6CC5075461678E3A62D23A2E55EA2D5A6 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:17190262 dynCall_vii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21636298 dynCall_vii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:28135 invoke_vii blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:18854 _ReversePInvokeWrapper_WebSocketFactory_DelegateOnOpenEvent_mB0CF6AB6CC5075461678E3A62D23A2E55EA2D5A6 blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:17190075 dynCall_vi blob:null/c587e4f0-fc76-436f-ab17-553632bb414b line 1394 > WebAssembly.instantiate:21635446 dynCall_vi blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:27985 dynCall blob:null/c587e4f0-fc76-436f-ab17-553632bb414b:579 onopen

cyinwind commented 4 years ago

补充: 修改的代码在LuaEnv.cs中,如下: public void Tick() {

if THREAD_SAFE || HOTFIX_ENABLE

        lock (luaEnvLock)
        {

endif

            var _L = L;
            //lock (refQueue)
            //{
                while (refQueue.Count > 0)
                {
                    GCAction gca = refQueue.Dequeue();
                    translator.ReleaseLuaBase(_L, gca.Reference, gca.IsDelegate);
                }
            //}

if !XLUA_GENERAL

            last_check_point = translator.objects.Check(last_check_point, max_check_per_tick, object_valid_checker, translator.reverseMap);

endif

if THREAD_SAFE || HOTFIX_ENABLE

        }

endif

    }

LockFreeQueue refQueue = new LockFreeQueue(); internal void equeueGCAction(GCAction action) { refQueue.Enqueue(action); // lock (refQueue) // { // refQueue.Enqueue(action); // } }

chexiongsheng commented 4 years ago

浏览器执行都是单线程。感觉直接加个条件编译宏,如果是WEBGL就别lock就可以了

cyinwind commented 4 years ago

用非锁互斥队列性能也比Lock好点,所以我就替换了

cyinwind commented 4 years ago

另外,我能问问,为啥 webgl 不用 luajit么?这个支持方便么?

chexiongsheng commented 4 years ago

lua是c写的,c支持的平台就支持。 luajit部分代码是用汇编写的,luajit写了哪个平台的就支持哪个平台。所以这个问题问luajit作者比较合适。

linkliu commented 2 years ago

@cyinwind 大佬,我把你的LockFreeQueue代码拉取下来,但是报错了,请问我可以参考一下你的代码吗 image

linkliu commented 2 years ago

@cyinwind 大佬,我把你的LockFreeQueue代码拉取下来,但是报错了,请问我可以参考一下你的代码吗 image

解决了,我把代码改成下面的样子就没有问题了

using System.Threading;

namespace XLua
{
    public class LockFreeQueue<T>
    {
        internal class SingleLinkNode<U> where U : T
        {
            public SingleLinkNode<U> Next;
            public U Item;
        }

        static private bool CAS<T>(ref T location, T comparand, T newValue) where T : class
        {
            return comparand == Interlocked.CompareExchange(ref location, newValue, comparand);
        }

        SingleLinkNode<T> head;
        SingleLinkNode<T> tail;
        int count;

        public int Count
        {
            get { return count; }
        }

        public bool IsEmpty
        {
            get { return count <= 0; }
        }

        public LockFreeQueue()
        {
            head = new SingleLinkNode<T>();
            tail = head;
            count = 0;
        }

        public void Enqueue(T item)
        {
            SingleLinkNode<T> oldTail = null;
            SingleLinkNode<T> oldTailNext;

            SingleLinkNode<T> newNode = new SingleLinkNode<T>();
            newNode.Item = item;

            bool newNodeAdded = false;
            while (!newNodeAdded)
            {
                oldTail = tail;
                oldTailNext = oldTail.Next;

                if (tail == oldTail)
                {
                    if (oldTailNext == null)
                        newNodeAdded = CAS<SingleLinkNode<T>>(ref tail.Next, null, newNode);
                    else
                        CAS<SingleLinkNode<T>>(ref tail, oldTail, oldTailNext);
                }
            }

            CAS<SingleLinkNode<T>>(ref tail, oldTail, newNode);
            Interlocked.Increment(ref count);
        }

        public bool TryDequeue(out T item)
        {
            item = default(T);
            SingleLinkNode<T> oldHead = null;

            bool haveAdvancedHead = false;
            while (!haveAdvancedHead)
            {
                oldHead = head;
                SingleLinkNode<T> oldTail = tail;
                SingleLinkNode<T> oldHeadNext = oldHead.Next;

                if (oldHead == head)
                {
                    if (oldHead == oldTail)
                    {
                        if (oldHeadNext == null)
                        {
                            return false;
                        }

                        CAS<SingleLinkNode<T>>(ref tail, oldTail, oldHeadNext);
                    }
                    else
                    {
                        item = oldHeadNext.Item;
                        haveAdvancedHead = CAS<SingleLinkNode<T>>(ref head, oldHead, oldHeadNext);
                    }
                }
            }

            Interlocked.Decrement(ref count);
            return true;
        }

        public T Dequeue()
        {
            T result;
            if (TryDequeue(out result)) return result;
            return default(T);
        }

        public void Clear()
        {
            while (Count > 0)
            {
                Dequeue();
            }
        }
    }
}
ripwu commented 1 year ago

浏览器执行都是单线程。感觉直接加个条件编译宏,如果是WEBGL就别lock就可以了

@chexiongsheng 测试了下,不一定对。这个 issue 和多线程无关。主要是因为 GC 时调用了 Dispose,它会调用 refQueue.Enqueue,而这可能会导致 refQueue 扩容。为了满足扩容的内存需求,refQueue.Enqueue 可能会再次引起 GC,导致调用第二个 LuaBaseDispose,它又再次调用 refQueue.Enqueue。也就是说 refQueue.Enqueue 可能导致 Queue.SetCapacity 的重入 (表现上像是多线程并发),因而 Destination array was not long enough

至于重入,可能是因为 lock (refQueue) 的锁 Monitor 是个递归锁,同个线程可以重入,而不会死锁。参考 MSDN 文档

It is legal for the same thread to invoke Enter more than once without it blocking; however, an equal number of Exit calls must be invoked before other threads waiting on the object will unblock.

一种解决方案是上面大佬发的使用 CAS 实现 LockFreeQueue

(如果有使用 tolua 的童鞋找到这个 issue)tolua 同样存在这个问题,但 tolua 是 gcList,在 tolua 要实现 LockFreeList 会相对麻烦些,因为还要支持在任意节点 RemoveAt() 的需求。如果只想简单的 fix,可以在 gcList 初始化时分配足够的 capacity,如 List<GCRef> gcList = new List<GCRef>(4096);,其中的 4096 是拍脑袋的,原则是 gcList 不扩容即可。

xLua 这个 issue 可能也可以这样绕过。

klistin commented 10 months ago

@ripwu 那按照你这个说法,之前的android或者IOS平台也会报这个错误才对,为什么没有呢?

lybmccree commented 5 months ago

微信小游戏 webgl 也遇到类似 array.copy爆了, websocket调用过来,异步,也GC炸锅了,XLua原版,啥都没改也有问题,不是只有主线程吗,不懂,求大神指导

lybmccree commented 4 months ago

浏览器执行都是单线程。感觉直接加个条件编译宏,如果是WEBGL就别lock就可以了

@chexiongsheng 测试了下,不一定对。这个 issue 和多线程无关。主要是因为 GC 时调用了 Dispose,它会调用 refQueue.Enqueue,而这可能会导致 refQueue 扩容。为了满足扩容的内存需求,refQueue.Enqueue 可能会再次引起 GC,导致调用第二个 LuaBaseDispose,它又再次调用 refQueue.Enqueue。也就是说 refQueue.Enqueue 可能导致 Queue.SetCapacity 的重入 (表现上像是多线程并发),因而 Destination array was not long enough

至于重入,可能是因为 lock (refQueue) 的锁 Monitor 是个递归锁,同个线程可以重入,而不会死锁。参考 MSDN 文档

It is legal for the same thread to invoke Enter more than once without it blocking; however, an equal number of Exit calls must be invoked before other threads waiting on the object will unblock.

一种解决方案是上面大佬发的使用 CAS 实现 LockFreeQueue

(如果有使用 tolua 的童鞋找到这个 issue)tolua 同样存在这个问题,但 tolua 是 gcList,在 tolua 要实现 LockFreeList 会相对麻烦些,因为还要支持在任意节点 RemoveAt() 的需求。如果只想简单的 fix,可以在 gcList 初始化时分配足够的 capacity,如 List<GCRef> gcList = new List<GCRef>(4096);,其中的 4096 是拍脑袋的,原则是 gcList 不扩容即可。

xLua 这个 issue 可能也可以这样绕过。

像webgl微信小游戏同一个主线程,不是有顺序的吗,重入,也不应该会导致这个问题出现,虽然现实中确实坑爹的出现了这个问题,无解~

klpk commented 3 months ago

最近刚把项目做了点修改测一下微信小游戏,也遇到了,引擎是团结1.2.2 plugin.js:93 ArgumentException: Destination array was not long enough. Check destIndex and length, and the array's lower bounds Parameter name: destinationArray at System.Array.Copy (System.Array sourceArray, System.Int32 sourceIndex, System.Array destinationArray, System.Int32 destinationIndex, System.Int32 length) [0x00000] in <00000000000000000000000000000000>:0

(env: Windows,mg,1.06.2405020; lib: 3.2.5) e.printErr @ plugin.js:93 _JS_Log_Dump @ webgl.wasm.framework.unityweb.js:1262 WebGLPrintfConsolev(LogType, char const, void) @ 0b10b0a6:0xbb9b8 InternalErrorConsole(char const, ...) @ 0b10b0a6:0x205b025 DebugStringToFilePostprocessedStacktrace(DebugStringToFileData const&) @ 0b10b0a6:0x205ae2b DebugStringToFile(DebugStringToFileData const&) @ 0b10b0a6:0x205a4e3 Scripting::LogExceptionFromManaged(ScriptingExceptionPtr, int, char const, bool, Scripting::LogExceptionFromMangedSettings const) @ 0b10b0a6:0x1f79f2f DebugLogHandler_CUSTOM_Internal_LogException(Il2CppObject, Il2CppObject) @ 0b10b0a6:0x1f95632 DebugLogHandler_LogException_mF66A663A86BF5D412BC9A4B3F92DA5CF61049F08 @ 0b10b0a6:0xba295e Logger_LogException_m591AF39F0886DA44666068EDBBD3CCF07623CFBB @ 0b10b0a6:0x15cae9 Debug_LogException_mAB3F4DC7297ED8FBB49DAA718B70E59A6B0171B0 @ 0b10b0a6:0xb9d117 U3CU3Ec_U3CRegisterUECatcherU3Eb__0_0_mD21DF44CEC3DA35516DA5FC75CA8A2FF67E4E382 @ 0b10b0a6:0xd49f6c KeyGeneratedEventHandler_Invoke_mD73F8C7BC0305037EC520C3F4AF3DE74E5481DDA @ 0b10b0a6:0x2430d8 RuntimeInvoker_TrueVoid_t4861ACF8F4594C3437BB48B6E56783494B843915_VoidU2A_t104EAEFBD2D237A8C29618913DA9B4D99355E965_VoidU2A_t104EAEFBD2D237A8C29618913DA9B4D99355E965(void ()(), MethodInfo const, void, void*, void) @ 0b10b0a6:0x1398de4 il2cpp::vm::Runtime::InvokeWithThrow(MethodInfo const, void, void) @ 0b10b0a6:0x13cfad3 dynCall_iiii @ 0b10b0a6:0x206520a invoke_iiii @ webgl.wasm.framework.unityweb.js:1277 il2cpp::vm::Runtime::Invoke(MethodInfo const, void, void, Il2CppException*) @ 0b10b0a6:0x13cea8a il2cpp::vm::Runtime::UnhandledException(Il2CppException) @ 0b10b0a6:0x13d018d il2cpp::gc::GarbageCollector::RunFinalizer(void, void) @ 0b10b0a6:0x101865 GC_try_to_collect_general @ 0b10b0a6:0xc707c GC_gcollect @ 0b10b0a6:0xc7108 il2cpp_gc_collect_a_little @ 0b10b0a6:0x13e8122 MainLoop() @ 0b10b0a6:0x1e88cf4 dynCall_v @ 0b10b0a6:0x2065228 browserIterationFunc @ webgl.wasm.framework.unityweb.js:1277 callUserCallback @ webgl.wasm.framework.unityweb.js:1277 runIter @ webgl.wasm.framework.unityweb.js:1277 Browser_mainLoop_runner @ webgl.wasm.framework.unityweb.js:1275

SiMaLaoShi commented 2 months ago

tolua也遇到了