drawNumberOfQuads crash on multiple OpenGl contexts

Hi guys! We get thousands of crashes in our app as follows:

01%20AM

I am in no way an expert in OpenGl and rendering but i believe the problem happens when multiple gl contexts are involved. We verified that this crash happens after playable rewarded videos appear on screen and are closed and cocos2d-x is trying to continue rendering. I guess this was also a problem of the past in this method:

-(void) doCaller: (id) sender{
if (isAppActive) {
    cocos2d::Director* director = cocos2d::Director::getInstance();
    EAGLContext* cocos2dxContext = [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context];
    if (cocos2dxContext != [EAGLContext currentContext])
        glFlush();

    [EAGLContext setCurrentContext: cocos2dxContext];

    CFTimeInterval dt = ((CADisplayLink*)displayLink).timestamp - lastDisplayTime;
    lastDisplayTime = ((CADisplayLink*)displayLink).timestamp;
    director->mainLoop(dt);
}

}

As i see we check if current context is cocos2d-x and flush it before setting the context. Is there any other improvement that can be done in this function to safeguard it further?

  1. Will it make any difference using glFinish instead of glFlush?
  2. Will it make any difference to getEAGLView after the glFlush?

I am asking this becasue this fix was originated from cocos crashed due to opengl context conflict when multiple contexts coexisted · Issue #16873 · cocos2d/cocos2d-x · GitHub in which the solution proposed was different:

-(void) doCaller: (id) sender
    {
    if (isAppActive) {
         cocos2d::Director* director = cocos2d::Director::getInstance();
        if([EAGLContext currentContext] != [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context]) 
   {
        glFinish();
        
        [EAGLContext setCurrentContext: [(CCEAGLView*)director->getOpenGLView()->getEAGLView() context]];
    }
    
    director->mainLoop();
}
}

Any OpenGl/Cocos2d-x experts please help so that we can solve for good this issue.

@zhangxm do you recall this issue?

@kerryk glFinish will block the app, and it is more safe if the issue is caused by the multiple gl context conflict as you said. Can you reproduce the issue?

@zhangxm the issue is very difficult to reproduce locally because you need to get the right ad and good timing

I see. You can replace glFlush with glFinish, it will not cause error, just may block game a while.

1 Like

Ok thank you very much i will give it a try!

@kerryk any update?

On android we have similar crashes Cocos2d 3.17.1 crash on android

Unfortunalty, we didnt give it a try at the end. However, we have managed to found which specific ad provider was causing cocos to crash and blcoked those ads.

which ad network was causing the issue for you we are also facing the similar crash on Android. If you can help identifying the ad Network then it would be great.

I remember that it was playable ads 100%. I dont remember well network but i believe was mopub.

If somebody can provide a demo to reproduce it. I will take a look.