views:

878

answers:

3

Hello, 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,
                  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.

A: 

[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);
Jason Webb
thanks, will try!
x0661t
textureImage[0] = [[UIImage alloc] initWithContentsOfFile: fileName];this line causing warning: assignment from incompatible pointer type. btw, textureImage is CGImageRef type.
x0661t
What OS do you see leaks under BigJason? Have you reported this to Apple?
Shaggy Frog
how can i release this thing: textureImage[9] = [[[UIImage alloc] initWithContentsOfFile: fileName9] CGImage]; ?
x0661t
anyone can help me?
x0661t
I edited my post to be more specific to what you are doing.
Jason Webb
you mean: CGImageRef cg = [m_image CGImage]; ? Cause There is no type called CGImage, or maybe i'm wrong?
x0661t
BigJason, how can i contact you? Could you help me to selve this problem?
x0661t
Is the code from my example not working for you? Create the UIImage. Grab the DBImageRef. Release the UIImage. Add the CGImageRef to your array. When done with it call CGImageRelease. There should be no more memory leaks if you follow that.
Jason Webb
i did exactly what you said, nothing happens to my leak, it's still present :(
x0661t
who can help me?
x0661t
A: 

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.

Chris Garrett
Hi, i'm not trying to access any element of textureImage outside this method. It's only here. Any suggestions?
x0661t
maybe i have to use glDeleteTextures() in the dealloc method? Just cant understand how to use it :(
x0661t
A: 

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]);
    }
x0661t