Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Crash When Disposing ScriptPromise instance. #45

Open
jo32 opened this issue Aug 5, 2021 · 3 comments
Open

Crash When Disposing ScriptPromise instance. #45

jo32 opened this issue Aug 5, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@jo32
Copy link

jo32 commented Aug 5, 2021

I wrote some code like:

var promise = new TypedScriptPromise<JSValue>(sc);
System.Action<object> callback = (object o) => {
    promise.Resolve(JSApi.JS_NULL);
};

and it crashed on this line:
https://github.com/ialex32x/unity-jsb/blob/master/Assets/jsb/Source/ScriptRuntime.cs#L611

@ialex32x
Copy link
Owner

ialex32x commented Aug 6, 2021

Hi jo32, can you give more detail about the crash? I can't reproduce it in my environment. BTW, the generic parameter T of TypedScriptPromise<> is expected as a C# type. If there is no need to return anything back to js, use AnyScriptPromise.

(I added a test in Assets/Examples/Source/SampleBehaviour.cs: AnotherWait())

    public AnyScriptPromise AnotherWait(ScriptContext ctx, int t)
    {
        var p = new AnyScriptPromise(ctx);
        StartCoroutine(_WaitForResolve(() => p.Resolve()));
        return p;
    }

    private System.Collections.IEnumerator _WaitForResolve(System.Action p)
    {
        yield return new WaitForSeconds(3f);
        p();
    }

@ialex32x ialex32x added the bug Something isn't working label Aug 6, 2021
@jo32
Copy link
Author

jo32 commented Aug 11, 2021

Sorry for not able providing enough example for you, I am able to solve this problem by removing one line in ScriptPromise.cs:

        protected virtual void Dispose(bool bManaged)
        {
            if (_context != null)
            {
                var context = _context;
                _context = null;
                // TODO: find out why the following two lines solve the crash problem
                context.FreeValue(_promise);
                // context.FreeValues(_resolving_funcs);
                _resolving_funcs = null;
                _promise = JSApi.JS_UNDEFINED;
            }
        }

@ialex32x
Copy link
Owner

ialex32x commented Aug 13, 2021

Hi jo32, which commit are you currently using? It looks different from my version?

        protected virtual void Dispose(bool bManaged)
        {
            if (_context != null)
            {
                var context = _context;
                _context = null;
                context.FreeValues(_resolving_funcs);
                _resolving_funcs = null;
                context.GetRuntime().FreeScriptPromise(_promise);
                _promise = JSApi.JS_UNDEFINED;
            }
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants