开发者

Memory leak problem. iPhone SDK

i've got a problem, i cannot solve it, just recieving error:

Program received signal: “0”.

The Debugger has exited due to signal 10 (SIGBUS).The Debugger has exited due to signal 10 (SIGBUS).

Here is some method, if i comment it out, problem goes aways

- (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,
          开发者_StackOverflow中文版        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);
     }
 }

anyone can help me with releasing/deleting objects in this method? Thanks.


[UIImage imagenamed:] creates memory leaks often. I had a similar issue and fixed it by using the method found here. Basically you want to use something like this:

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);


Are you attempting to access any of the elements in the textureImage array outside the context of this method?

The elements in that array are being allocated and autoreleased by imageNamed:, but it looks like textureImage is defined outside the method. If you attempt to call a method on any of the images in that array later on, you would see the error you're seeing.

If you do need the elements in the array elsewhere, be sure to retain the CGImage or UIImage objects here, and release them when you're done.


for all who will have such problem, just release your textures in dealloc, here is an example:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜