0

我有一个问题,我无法解决它,只是收到错误:

节目接收信号:“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);
     }
 }

任何人都可以帮助我在此方法中释放/删除对象吗?谢谢。

4

3 回答 3

0

[UIImage imagenamed:] 经常造成内存泄漏。我遇到了类似的问题,并使用此处找到的方法进行了修复。基本上你想使用这样的东西:

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);
于 2010-04-26T22:00:25.973 回答
0

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

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

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

于 2010-04-30T00:37:13.527 回答
0

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

    for(int i=0; i<10; i++) {
        glDeleteTextures(1, &textures[i]);
    }
于 2010-04-30T13:00:38.093 回答