تعطل البرنامج عندما يطلق UIImage بعد توليد برنامج OpenGL ES الملمس

StackOverflow https://stackoverflow.com/questions/1411410

  •  05-07-2019
  •  | 
  •  

سؤال

والآن، لقد تابعت هذا البرنامج التعليمي:

HTTP: // iphonedevelopment .blogspot.com / 2009/05 / برنامج OpenGL ES-من الأرض تصل لجزء من 6_25.html

وأنها لا تعمل إذا حاولت أن الإفراج UIImage: أنا سوف تحصل على "EXC_BAD_ACCESS. الآن، أنا جعلت UIImage أن تكون المتغيرات المثال والاحتفاظ بها. أنا لا تجعل من autorelease. ولكن المشكلة هي في بعض الأحيان أريد حذف بلدي الملمس. ولست بحاجة إلى إطلاق سراح أولئك UIImage وإلا سوف يكون لي تسرب. (وتفيد التسريبات أداة الأداء التي UIImage يسبب تسرب) ولكن إذا كان إطلاق سراح UIImage، وسوف تحصل على EXC_BAD_ACCESS. وأنا لا حتى رسم لهم أو الحصول عليها في أي مكان على الإطلاق. البرنامج تعطل فقط الحق في حيث أنها حصلت على الافراج عن:

#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
...
...

والخط 329 في Texture.m هو المكان الذي يمكنني الكشف عن بلدي UIImage.

ورمزي مختلف قليلا من البرنامج التعليمي ولكن يجب أن تعمل بطريقة مماثلة جدا:

- (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;
}

إذا يمكنك ان ترى ما هو الخطأ، يرجى توجيه لي. شكرا لك.

هل كانت مفيدة؟

المحلول

وإزالة بطريقة أو بأخرى

CGImageRelease(image)

وإصلاح المشكلة. يمكنني إزالة UIImage بعد ذلك دون أي مشكلة وليس هناك تسرب أيضا.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top