cocos2d / cocos2d-x

Cocos2d-x is a suite of open-source, cross-platform, game-development tools utilized by millions of developers across the globe. Its core has evolved to serve as the foundation for Cocos Creator 1.x & 2.x.
https://www.cocos.com/en/cocos2d-x
18.22k stars 7.06k forks source link

Memory leaks caused by static field in Cocos2dx related class #17864

Open jsonchi opened 7 years ago

jsonchi commented 7 years ago

I have an Android App which embed Cocos Activity, like most Activities in my App is normal Android Activity except a few of Cocos Activities which inherited from Cocos2dxActivity.

When I quit from Cocos Activity and back to normal Activity,the static field(e.g. sContext) in Cocos2dxActivity can cause memory leaks.

So how I can do to fix this kind of issue?

Dauch commented 7 years ago

which method do you use to measure? I remember if you hold sContext as static, that will cause memory leaks.

mars3142 commented 7 years ago

@jsonchi I think cocos2d-x isn't supposed to be a multi activity app. Because of that the

private static Cocos2dxActivity sContext = null;

was implemented to be useable with

public static Context getContext() {
    return sContext;
}

The only possible way, I see right now, is to clear the sContext value by yourself, if you close the activity.

PS: Just an idea, would it be possible to set the variable in onStart and clear it in onStop? I don't know the the getContext() call is everywhere used within cocos2d-x (without searching all the code).

minggo commented 7 years ago

sContext is used as static variable is because JNI codes will use the variable, and currently, all native method are static.

Is sContext the only thing that will cause memory leak?

jsonchi commented 7 years ago

Open GL drawing state also influenced when re-enter that Activty