Cocos2d-x异步加载函数addImageAsync存在内存释放不完全的问题:
解决方案:【http://www.cocos2d-x.org/forums/6/topics/17031】
修改CCTextureCache中的静态方法:loadImage(void * data)
的
CCThread thread;
thread.createAutoreleasePool();
这两句放在while()循环里:
static void* loadImage(void* data)
{
// create autorelease pool for iOS
//CCThread thread; //郭佳修改2013年12月5日 解决异步加载addImageAsync内存泄露leaks的问题
//thread.createAutoreleasePool();
AsyncStruct *pAsyncStruct = NULL;
while(true)
{
CCThread thread;
thread.createAutoreleasePool();
// wait for rendering thread to ask for loading if s_pAsyncStructQueue is empty
int semWaitRet = sem_wait(s_pSem);
if( semWaitRet < 0 )
{
CCLOG( “CCTextureCache async thread semaphore error: %s\n”, strerror( errno ) );
break;
}
std::queue<AsyncStruct*> *pQueue = s_pAsyncStructQueue;
pthread_mutex_lock(&s_asyncStructQueueMutex);// get async struct from queue
if (pQueue->empty())
{
pthread_mutex_unlock(&s_asyncStructQueueMutex);
if (need_quit)
break;
else
continue;
}
else
{
pAsyncStruct = pQueue->front();
pQueue->pop();
pthread_mutex_unlock(&s_asyncStructQueueMutex);
}
const char *filename = pAsyncStruct->filename.c_str();
// compute image type
CCImage::EImageFormat imageType = computeImageFormatType(pAsyncStruct->filename);
if (imageType == CCImage::kFmtUnKnown)
{
CCLOG(“unsupported format %s”,filename);
delete pAsyncStruct;
continue;
}
// generate image
CCImage *pImage = new CCImage();
if (! pImage->initWithImageFileThreadSafe(filename, imageType))
{
delete pImage;
CCLOG(“can not load %s”, filename);
continue;
}
// generate image info
ImageInfo *pImageInfo = new ImageInfo();
pImageInfo->asyncStruct = pAsyncStruct;
pImageInfo->image = pImage;
pImageInfo->imageType = imageType;
// put the image info into the queue
pthread_mutex_lock(&s_ImageInfoMutex);
s_pImageQueue->push(pImageInfo);
pthread_mutex_unlock(&s_ImageInfoMutex);
}
if( s_pSem != NULL )
{
#if CC_ASYNC_TEXTURE_CACHE_USE_NAMED_SEMAPHORE
sem_unlink(CC_ASYNC_TEXTURE_CACHE_SEMAPHORE);
sem_close(s_pSem);
#else
sem_destroy(s_pSem);
#endif
s_pSem = NULL;
delete s_pAsyncStructQueue;
delete s_pImageQueue;
}
return 0;
}