Fix "TextureCache Test" in js-test crash under Android

This commit is contained in:
XiaoFeng 2015-11-06 09:49:19 +08:00
parent ac0a0ed3bb
commit 1a56668cd3
3 changed files with 18 additions and 3 deletions

View File

@ -43,6 +43,7 @@ namespace cocos2d { namespace network {
static int sTaskCounter; static int sTaskCounter;
static int sDownloaderCounter; static int sDownloaderCounter;
static bool _registered = false;
struct DownloadTaskAndroid : public IDownloadTask struct DownloadTaskAndroid : public IDownloadTask
{ {
@ -53,7 +54,6 @@ namespace cocos2d { namespace network {
} }
virtual ~DownloadTaskAndroid() virtual ~DownloadTaskAndroid()
{ {
DLLOG("Destruct DownloadTaskAndroid: %p", this); DLLOG("Destruct DownloadTaskAndroid: %p", this);
} }
@ -66,7 +66,8 @@ namespace cocos2d { namespace network {
, _impl(nullptr) , _impl(nullptr)
{ {
// use local static variable make sure native methods registered once // use local static variable make sure native methods registered once
static bool _registered = _registerNativeMethods(JniHelper::getEnv()); if(!_registered)
_registered = _registerNativeMethods(JniHelper::getEnv());
DLLOG("Construct DownloaderAndroid: %p", this); DLLOG("Construct DownloaderAndroid: %p", this);
JniMethodInfo methodInfo; JniMethodInfo methodInfo;
if (JniHelper::getStaticMethodInfo(methodInfo, if (JniHelper::getStaticMethodInfo(methodInfo,
@ -163,6 +164,7 @@ namespace cocos2d { namespace network {
} }
DownloadTaskAndroid *coTask = iter->second; DownloadTaskAndroid *coTask = iter->second;
string str = (errStr ? errStr : ""); string str = (errStr ? errStr : "");
_taskMap.erase(iter);
onTaskFinish(*coTask->task, onTaskFinish(*coTask->task,
errStr ? DownloadTask::ERROR_IMPL_INTERNAL : DownloadTask::ERROR_NO_ERROR, errStr ? DownloadTask::ERROR_IMPL_INTERNAL : DownloadTask::ERROR_NO_ERROR,
errCode, errCode,
@ -170,7 +172,15 @@ namespace cocos2d { namespace network {
data data
); );
coTask->task.reset(); coTask->task.reset();
_taskMap.erase(iter); }
void _preloadJavaDownloaderClass()
{
if(!_registered)
{
_registered = _registerNativeMethods(JniHelper::getEnv());
}
} }
} }
} // namespace cocos2d::network } // namespace cocos2d::network

View File

@ -50,5 +50,8 @@ namespace cocos2d { namespace network
std::unordered_map<int, DownloadTaskAndroid*> _taskMap; std::unordered_map<int, DownloadTaskAndroid*> _taskMap;
}; };
// Load java downloader class from main thread, to void addImageAsync function
// perform in sub-thread cause java downloader unusable
void _preloadJavaDownloaderClass();
}} // namespace cocos2d::network }} // namespace cocos2d::network

View File

@ -36,6 +36,7 @@ THE SOFTWARE.
#include "renderer/ccGLStateCache.h" #include "renderer/ccGLStateCache.h"
#include "2d/CCDrawingPrimitives.h" #include "2d/CCDrawingPrimitives.h"
#include "platform/android/jni/JniHelper.h" #include "platform/android/jni/JniHelper.h"
#include "network/CCDownloader-android.h"
#include <android/log.h> #include <android/log.h>
#include <jni.h> #include <jni.h>
@ -81,6 +82,7 @@ JNIEXPORT void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, j
director->getEventDispatcher()->dispatchEvent(&recreatedEvent); director->getEventDispatcher()->dispatchEvent(&recreatedEvent);
director->setGLDefaultValues(); director->setGLDefaultValues();
} }
cocos2d::network::_preloadJavaDownloaderClass();
} }
JNIEXPORT jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs(JNIEnv* env, jobject thiz) JNIEXPORT jintArray Java_org_cocos2dx_lib_Cocos2dxActivity_getGLContextAttrs(JNIEnv* env, jobject thiz)