生成 OpenGL ES 纹理后释放 UIImage 时程序崩溃 [英] Program crashes when releases UIImage after generating OpenGL ES texture

查看:117
本文介绍了生成 OpenGL ES 纹理后释放 UIImage 时程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我遵循了本教程:

Right now, I have followed this tutorial:

http://iphonedevelopment.blogspot.com/2009/05/opengl-es-from-ground-up-part-6_25.html

如果我试图释放 UIImage,它就不起作用:我会得到一个EXC_BAD_ACCESS".现在,我将 UIImage 设为实例变量并保留它们.我不会让它自动释放.但问题是有时我想删除我的纹理.我需要释放那些 UIImage 否则我会泄漏.(性能工具泄漏报告 UIImage 导致泄漏)但如果我释放 UIImage,我将获得 EXC_BAD_ACCESS.我什至没有在任何地方绘制它们或访问它们.程序就在它发布的地方崩溃了:

It does not work if I tried to release UIImage: I will get a 'EXC_BAD_ACCESS'. Now, I made the UIImage to be instance variables and retain them. I do not make it autorelease. But the problem is sometimes I want to delete my texture. I need to release those UIImage or else I will have a leak. (The Performance Tool Leaks report that UIImage causes leaks) But if I release UIImage, I will get EXC_BAD_ACCESS. And I am not even drawing them or accessing them anywhere at all. The program just crashes right at where it got release:

#0  0x30011940 in objc_msgSend ()
#1  0x302395f4 in CFGetTypeID ()
#2  0x308f480c in -[UIImage(UIImageDeprecated) imageRef] ()
#3  0x308f4ae0 in SharedIdentifierForImage ()
#4  0x308f4a30 in _UISharedImageDealloc ()
#5  0x308f4964 in -[UIImage dealloc] ()
#6  0x30236b78 in -[NSObject release] ()
#7  0x0000a374 in -[Texture dealloc] (self=0x184b30, _cmd=0x300f7fd0) at /Users/akaraphan/Desktop/Competition/TrapRoom/Classes/Texture.m:329
#8  0x30236b78 in -[NSObject release] ()
#9  0x30235f24 in CFRelease ()
#10 0x302043bc in __CFTypeCollectionRelease ()
#11 0x30205dac in __CFArrayReleaseValues ()
#12 0x30205c18 in __CFArrayDeallocate ()
#13 0x30236020 in _CFRelease ()
#14 0x30235efe in CFRelease ()
#15 0x3054836a in -[NSCFArray release] ()
#16 0x00011658 in -[GameSprite dealloc] (self=0x1838d0, _cmd=0x300f7fd0) at /Users/akaraphan/Desktop/Competition/TrapRoom/Classes/GameSprite.m:40
...
...

Texture.m 中的第 329 行是我发布 UIImage 的地方.

Line 329 in Texture.m is where I release my UIImage.

我的代码与教程略有不同,但应该非常相似:

My code is a little different from the tutorial but it should be working very similarly:

- (id) initFromImage: (NSString*)imageFile{

    if (self = [super init]){

        path = [[NSBundle mainBundle] pathForResource:imageFile ofType:@"png"];
        texData = [[NSData alloc] initWithContentsOfFile:path];
        img = [[UIImage alloc] initWithData:texData];

        CGImageRef image = img.CGImage;

        width = CGImageGetWidth(image);
        height = CGImageGetHeight(image);

        if (image){
            int tempWidth = (int)width, tempHeight = (int)height;

            if ((tempWidth & (tempWidth - 1)) != 0 ){
                NSLog(@"CAUTION! width is not power of 2. width == %d", tempWidth);
            }else if ((tempHeight & (tempHeight - 1)) != 0 ){
                NSLog(@"CAUTION! height is not power of 2. height == %d", tempHeight);
            }else{
                GLubyte *spriteData = (GLubyte*) calloc(width * 4, height * 4);

                CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4, CGImageGetColorSpace(image), kCGImageAlphaPremultipliedLast);

                CGContextDrawImage(spriteContext, CGRectMake(0.0, 0.0, width, height), image);

                CGContextRelease(spriteContext);

                glGenTextures(1, &GLtexture);

                glBindTexture(GL_TEXTURE_2D, GLtexture);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, spriteData);

                free(spriteData);

                CGImageRelease(image);

                NSLog(@"Texture %d", GLtexture);

            }

        }else NSLog(@"ERROR: Image not loaded...");

        return self;
    }
    return nil;
}

如果你能看出哪里出了问题,请指导我.谢谢.

If you can see what is wrong, please guide me. Thank you.

推荐答案

以某种方式移除

CGImageRelease(image)

解决了这个问题.之后我可以毫无问题地删除 UIImage 并且也没有泄漏.

fixed the problem. I can remove UIImage after that without any trouble and there's no leak as well.

这篇关于生成 OpenGL ES 纹理后释放 UIImage 时程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆