Skip to content
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

[V3.7:JS][Android] Crashes when many times call Cocos2dxJavascriptJavaBridge.evalString("")! #13807

Open
dimaria11 opened this issue Sep 14, 2015 · 15 comments

Comments

@dimaria11
Copy link

@dimaria11 dimaria11 commented Sep 14, 2015

activity.runOnGLThread(new Runnable() {
@override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("cc.log("--------------------onAdLoaded");");
}
});

I run above code about 5-6 times, app is crashes!

@dimaria11 dimaria11 changed the title [V3.7][Android] Crashes when many times call Cocos2dxJavascriptJavaBridge.evalString("")! [V3.7:JS][Android] Crashes when many times call Cocos2dxJavascriptJavaBridge.evalString("")! Sep 15, 2015
@pandamicro

This comment has been minimized.

Copy link
Member

@pandamicro pandamicro commented Sep 19, 2015

You shouldn't directly run SpiderMonkey APIs (via ScriptingCore) in another thread (Runnable is a thread)

@dimaria11

This comment has been minimized.

Copy link
Author

@dimaria11 dimaria11 commented Sep 20, 2015

@dimaria11

This comment has been minimized.

Copy link
Author

@dimaria11 dimaria11 commented Sep 22, 2015

I commented Runnable thread but app is still crashes after go to onAdLoaded() several times:
publisherInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
super.onAdLoaded();
// activity.runOnGLThread(new Runnable() {
// @override
// public void run() {
Cocos2dxJavascriptJavaBridge.evalString("cc.log("-----------------onAdLoaded");");
// }
// });
}
};

@Zinitter

This comment has been minimized.

Copy link

@Zinitter Zinitter commented Jan 27, 2016

@pandamicro

How can we call javascript from java in Android with Cocos2d-JS v3.8.1?

I'm facing the same issue which crashed when calling Cocos2dxJavascriptJavaBridge.evalString

Thank you.

@minggo

This comment has been minimized.

Copy link
Contributor

@minggo minggo commented Jul 5, 2016

@pandamicro Runnable is not a thread, the codes run in GL thread, so it runs in the same thread as cocos2d-x.

@Zinitter

This comment has been minimized.

Copy link

@Zinitter Zinitter commented Jul 5, 2016

@minggo

How can we call javascript from java in Android with Cocos2d-JS v3.8.1?

I can call javascript in iOS Obj-C with these :

#include "cocos/scripting/js-bindings/manual/ScriptingCore.h"

jsval ret;
ScriptingCore::getInstance()->evalString("JavascriptFunction()", &ret);

Thank you.

@minggo

This comment has been minimized.

Copy link
Contributor

@minggo minggo commented Jul 5, 2016

@Zinitter i think @dimaria11 's operation is correct, should invoke it in activity.runOnGLThread(). But i don't know why it will crash, is there any crash log?

@Zinitter

This comment has been minimized.

Copy link

@Zinitter Zinitter commented Jul 5, 2016

@minggo

I have no crash log and i forgot what i do in the callback that caused crash since its around half year ago issue.

However, i have retest the code in a simple code and it works.

I create a button in JS
when press JS button, it call Java function testBridge
Java function testBridge will call JS functon javaCallback to create a button

//Java
public static void testBridge()
{
    _AppActivity.runOnGLThread(new Runnable()
    {
        @Override
        public void run()
        {
            Cocos2dxJavascriptJavaBridge.evalString("testJavaBridge2()");
        }
    });
}

//JS
function javaCallback
{
    cc.director.getRunningScene().layer.javaCallback();
}

layer.javaCallback function : ()
{
    //create another button
}

I found that this code also working

    Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() {
        @Override
        public void run() {
            Cocos2dxJavascriptJavaBridge.evalString("javaCallback()");
        }
    });

Reference
http://discuss.cocos2d-x.org/t/call-js-from-java/23676

So which one is better?
1._AppActivity
or
2. Cocos2dxGLSurfaceView

@minggo

This comment has been minimized.

Copy link
Contributor

@minggo minggo commented Jul 5, 2016

Both are ok.

@Zinitter

This comment has been minimized.

Copy link

@Zinitter Zinitter commented Jul 5, 2016

@minggo

Ok, thank you.

I didn't use Cocos2dxJavascriptJavaBridge.evalString after found out it caused crash.

Now i will try to use and if have any issue, i will report here.

@sanzenwin

This comment has been minimized.

Copy link

@sanzenwin sanzenwin commented Jan 11, 2017

I'm facing this issue too, it caused crash when call it at second times ,here is the log:

signal 11 (SIGSEGV) at 0x00000030 (code=1), thread 5098

@minggo

This comment has been minimized.

Copy link
Contributor

@minggo minggo commented Jan 11, 2017

@sanzenwin could you please provide a test case? Thanks.

@sanzenwin

This comment has been minimized.

Copy link

@sanzenwin sanzenwin commented Jan 11, 2017

Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() {
    @Override
    public void run() {
        Cocos2dxJavascriptJavaBridge.evalString("javaCallback()");
    }
});

I change my code to above, it works !!!
I'am testing Weibo's sso feature. Goto Weibo's AuthActivity from MainActivity , and then click cancel button to go back MainActivity, then call Cocos2dxJavascriptJavaBridge.evalString("onCancel") in MainActivity. just do it two times, it cause crash.

@wzpan

This comment has been minimized.

Copy link

@wzpan wzpan commented Jul 5, 2018

activity.runOnGLThread(new Runnable() {
@override
public void run() {
Cocos2dxJavascriptJavaBridge.evalString("cc.log("--------------------onAdLoaded");");
}
});

I run above code about 5-6 times, app is crashes!

The same to above. I will try to eval js via queueEvent like @sanzenwin rather than runOnGLThread.

@wzpan

This comment has been minimized.

Copy link

@wzpan wzpan commented Jul 5, 2018

@sanzenwin Actually runOnGLThread uses queueEvent as the underlying method to put runnable to be executed in GLThread.

@Override
    public void runOnGLThread(final Runnable pRunnable) {
        this.mGLSurfaceView.queueEvent(pRunnable);
    }

So I wonder why changing your code to use queueEvent can solve your problem instead of using runOnGLThread directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
6 participants
You can’t perform that action at this time.