ZhouWeikuan / cocos2d

cocos2d for android, based on cocos2d-android-0.82, and now ported from cocos2d-iphone 0.99.4. The googlecode address is here: http://code.google.com/p/cocos2d-android-1/ . There are several demos to watch.
610 stars 291 forks source link

CCTexture2D should not use finalize() to clean up GL Textures #44

Open chrisbtreats opened 12 years ago

chrisbtreats commented 12 years ago

Finalizers are not guaranteed to be called in a timely manner if at all and should not be used to release the GL textures.

I performed a local test of unloading the texture whenever CCTextureCache.removeTexture(CCTexture2D) and the native heap memory no longer grows and I no longer receive out of memory errors.

Per http://developer.android.com/reference/java/lang/Object.html#finalize%28%29 you should be using a ReferenceQueue to receive notifications when the WeakReference is garbage collected and perform the clean up then. You will most likely need to subclass WeakReference to store the "_name" value of the texture because it won't be available once the CCTexture2D is garbage collected.