我尝试在 cocos2d 的后台线程中渲染纹理,并且进展顺利,除了由于某种原因我无法在不再使用时释放纹理。
首先异步加载两个图像,然后我运行一个后台任务来渲染一个新图像。正如我所说的一切正常,问题是我的应用程序在调用这些函数几次后崩溃。我不知道如何做更多的清理工作。注销我的可用内存告诉我每次都会丢失 10-15 mb (gfx1 和 gfx2 是视网膜全屏背景)。
问题必须在这些代码行中,因为当我删除它们时,我不再有内存问题,并且分析我的应用程序说没有泄漏!
纹理是一个NSMutableArray
. 我在索引 0 处有一个纹理,渲染一个新纹理并将其添加到位置 1。替换精灵后,我尝试在索引 0 处杀死我的(现在旧的)纹理,我的新纹理变为索引 0,所以我可以运行这个功能重新来过。
所以这里是代码
- (void) startBuildingTextureInBackground {
[[CCTextureCache sharedTextureCache] addImageAsync:@"gfx1.png"
target:self
selector:@selector(imageLoaded:)];
}
- (void) imageLoaded: (id) obj {
rtxTexture1 = [[CCTextureCache sharedTextureCache] textureForKey:@"gfx1.png"];
[[CCTextureCache sharedTextureCache] addImageAsync:@"gfx2.png"
target:self
selector:@selector(imageLoaded2:)];
}
- (void) imageLoaded2: (id) obj {
rtxTexture2 = [[CCTextureCache sharedTextureCache] textureForKey:@"gfx2.png"];
[self performSelectorInBackground:@selector(buildRtxTexture) withObject:nil];
}
- (void) buildRtxTexture {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
EAGLSharegroup *sharegroup = [[[[CCDirector sharedDirector] openGLView] context] sharegroup];
EAGLContext *k_context = [[[EAGLContext alloc] initWithAPI:kEAGLRenderingAPIOpenGLES1 sharegroup:sharegroup] autorelease];
[EAGLContext setCurrentContext:k_context];
[[CCDirector sharedDirector] setGLDefaultValues];
CCSprite* gfx1 = [CCSprite spriteWithTexture:rtxTexture1];
[rendernode addChild:gfx1];
CCSprite* gfx2 = [CCSprite spriteWithTexture:rtxTexture2];
[rendernode addChild:gfx2];
CCRenderTexture* rtx = [CCRenderTexture renderTextureWithWidth:512
height:320
pixelFormat:kTexture2DPixelFormat_RGBA4444];
[rtx beginWithClear:0 g:0 b:0 a:0];
[rendernode visit];
[rtx end];
[rendernode removeChild:gfx1 cleanup:YES];
[rendernode removeChild:gfx2 cleanup:YES];
[[CCTextureCache sharedTextureCache] removeTexture:rtxTexture1];
[[CCTextureCache sharedTextureCache] removeTexture:rtxTexture2];
[EAGLContext setCurrentContext:nil];
[self performSelectorOnMainThread:@selector(textureLoaded:) withObject:rtx.sprite.texture waitUntilDone:YES];
[pool release];
}
- (void) textureLoaded:(CCTexture2D*) newTexture {
[textures addObject:newTexture];
}
- (void) replaceTexture {
if (rtxSprite != nil) {
[spriteDisplay removeChild:rtxSprite cleanup:YES];
[[CCTextureCache sharedTextureCache] removeTexture:[textures objectAtIndex:0]];
[textures removeObjectAtIndex:0];
}
rtxSprite = [CCSprite spriteWithTexture:[textures objectAtIndex:0]];
rtxSprite.scaleY = -1;
[spriteDisplay addChild: rtxSprite];
}