我只需要一些关于内存管理的帮助。我在屏幕上显示大约 500 帧。我的应用程序似乎在模拟器上运行良好,但在 iPad 上它在显示大约 450 帧后崩溃。由于内存不足,问题似乎来了。下面是我的代码的一部分。我是否正确地释放了对象,还是我需要做更多的事情?
- (void)drawBufferWidth:(int)width height:(int)height pixels:(unsigned char*)pixels
{
CGRect rect = CGRectInset(self.view.bounds, 0.0, 0.0);
UIImageView *img = [[UIImageView alloc] initWithFrame:rect];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef gtx = CGBitmapContextCreate(pixels, width, height, BitsPerComponent, BytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast);
CGImageRef myimage = CGBitmapContextCreateImage(gtx);
img.image = [UIImage imageWithCGImage:myimage];
CGContextRelease(gtx);
CGImageRelease(myimage);
CGColorSpaceRelease(colorSpace);
}
最佳答案
此方法似乎泄漏了您在第二行创建的 UIImageView
。您的 alloc
没有对应的 release
或 autorelease
。我说“出现”是因为我怀疑您没有包含所有方法(您没有向我们展示任何对您的 UIImageView 执行任何操作的代码...)。
您对各种 CGRef 的管理很好。
关于ios - 释放 Objective-C 中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15816520/