Skip to content

[Bug]: Node引擎,在命令回调中使用异步操作时,内存引用错误 0xC0000005 #322

@P-TNPC

Description

@P-TNPC

Describe the bug

cmd.setCallback 中使用异步,如果在 await 执行之后再尝试调用回调函数提供的参数(如 output)的方法(如 output.success()),会稳定触发 0xC0000005
似乎是因为await期间,这些对象生命周期提前结束,引用的内存被释放。

报错内容

ERROR [legacy-script-engine-nodejs] Raw Seh Exception:
ERROR [legacy-script-engine-nodejs] [0xC0000005:ntstatus] 0x%p 指令引用了 0x%p 内存。该内存不能为 %s。
ERROR [legacy-script-engine-nodejs] Parameter 0: 0x1
ERROR [legacy-script-engine-nodejs] Parameter 1: 0x1
ERROR [legacy-script-engine-nodejs] Fail in success!
ERROR [legacy-script-engine-nodejs] In API: CommandOutputClass::success
ERROR [legacy-script-engine-nodejs] In Plugin: MyAsyncPlugin
ERROR [legacy-script-engine-nodejs] Raw Seh Exception:
ERROR [legacy-script-engine-nodejs] [0xC0000005:ntstatus] 0x%p 指令引用了 0x%p 内存。该内存不能为 %s。
ERROR [legacy-script-engine-nodejs] Parameter 0: 0x8
ERROR [legacy-script-engine-nodejs] Parameter 1: 0x0
ERROR [legacy-script-engine-nodejs] Fail in getOriginName!
ERROR [legacy-script-engine-nodejs] In API: CommandOriginClass::getOriginName
ERROR [legacy-script-engine-nodejs] In Plugin: TestPlugin

To Reproduce

触发报错的代码

out.success()await 之后被调用,引发错误:

cmd.setCallback(async (_cmd, ori, out, inp) => {
    switch (inp.action) {
        case 'reset':
            // 先执行异步操作
            await clearListAsync();

            // 到此步时,out 对象可能已失效,调用 .success() 会访问无效内存
            out.success('列表已清空。'); 
            mc.broadcast(`玩家 ${ori.name} 清空了列表`); // undefined
            break;
    }
});

这样写没有问题

cmd.setCallback(async (_cmd, ori, out, inp) => {
    switch (inp.action) {
        case 'reset':
            // 无异常
            out.success('列表已清空。'); 
            mc.broadcast(`玩家 ${ori.name} 清空了列表`); 

            // 后执行异步操作
            await clearListAsync();
            break;
    }
});

Expected behavior

回调提供的参数应该在整个回调结束时再释放

Screenshots

No response

Platform

Windows 10

BDS Version

1.21.93.1

LeviLamina Version

1.4.2

LegacyScriptEngine Version

0.13.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions