内存泄漏问题。 iPhone SDK

时间:2010-04-26 21:48:54

标签: iphone sdk cocos2d-iphone signals sigbus

我遇到了问题,我无法解决,只是收到错误:

编程收到信号:“0”。

由于信号10(SIGBUS),调试器已退出。由于信号10(SIGBUS),调试器已退出。

这是一些方法,如果我发表评论,问题就会消失

- (void)loadTexture {
     const int num_tex = 10;
     glGenTextures(num_tex, &textures[0]);

     //TEXTURE #1
     textureImage[0] = [UIImage imageNamed:@"wonder.jpg"].CGImage;
            //TEXTURE #2
     textureImage[1] = [UIImage imageNamed:@"wonder.jpg"].CGImage;
     //TEXTURE #3
     textureImage[2] = [UIImage imageNamed:@"wall_eyes.jpg"].CGImage;
     //TEXTURE #4
     textureImage[3] = [UIImage imageNamed:@"wall.jpg"].CGImage;
     //TEXTURE #5
     textureImage[4] = [UIImage imageNamed:@"books.jpg"].CGImage;
     //TEXTURE #6
     textureImage[5] = [UIImage imageNamed:@"bush.jpg"].CGImage;
     //TEXTURE #7
     textureImage[6] = [UIImage imageNamed:@"mushroom.jpg"].CGImage;
     //TEXTURE #8
     textureImage[7] = [UIImage imageNamed:@"roots.jpg"].CGImage;
     //TEXTURE #9
     textureImage[8] = [UIImage imageNamed:@"roots.jpg"].CGImage;
     //TEXTURE #10
     textureImage[9] = [UIImage imageNamed:@"clean.jpg"].CGImage; 

     for(int i=0; i<num_tex; i++) {
      NSInteger texWidth = CGImageGetWidth(textureImage[i]);
      NSInteger texHeight = CGImageGetHeight(textureImage[i]);
      GLubyte *textureData = (GLubyte *)malloc(texWidth * texHeight * 4);

      CGContextRef textureContext = CGBitmapContextCreate(textureData,
                  texWidth, texHeight,
                  8, texWidth * 4,
                  CGImageGetColorSpace(textureImage[i]),
                  kCGImageAlphaPremultipliedLast);
      CGContextDrawImage(textureContext, CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight), textureImage[i]);
      CGContextRelease(textureContext);

      glBindTexture(GL_TEXTURE_2D, textures[i]);
      glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);

      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
      glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

      free(textureData);
     }
 }

任何人都可以帮我发布/删除此方法中的对象?感谢。

3 个答案:

答案 0 :(得分:0)

[UIImage imagenamed:]经常造成内存泄漏。我有一个类似的问题,并使用找到的here方法修复它。基本上你想要使用这样的东西:

NSString *fileName = [NSString stringWithFormat: @"%@/Image.png", [[NSBundle mainBundle] resourcePath]];
UIImage *tmp = [[UIImage alloc] initWithContentsOfFile: fileName];
CGImageRef cg = [tmp CGImage];
[tmp release];
//Do stuff
CGImageRelease(cg);

答案 1 :(得分:0)

您是否尝试访问此方法上下文之外的textureImage数组中的任何元素?

该数组中的元素正在由imageNamed:分配和自动释放,但看起来在该方法之外定义了textureImage。如果您稍后尝试在该数组中的任何图像上调用方法,您将看到您看到的错误。

如果你确实需要其他地方的数组中的元素,请确保在这里保留CGImage或UIImage对象,并在完成后释放它们。

答案 2 :(得分:0)

对于所有遇到此类问题的人,只需在dealloc中释放纹理,这是一个例子:

    for(int i=0; i<10; i++) {
        glDeleteTextures(1, &textures[i]);
    }
相关问题