gpt4 book ai didi

iphone - free() 调用在模拟器上有效,使 iPad 生气。 iPad粉碎

转载 作者:太空狗 更新时间:2023-10-30 03:32:37 27 4
gpt4 key购买 nike

我的应用内存不足。为了解决这个问题,我释放了两个非常大的数组,这些数组在将帧缓冲区写入图像的函数中使用。该方法如下所示:

-(UIImage *) glToUIImage {
NSInteger myDataLength = 768 * 1024 * 4;
// allocate array and read pixels into it.
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, 768, 1024, GL_RGBA, GL_UNSIGNED_BYTE, buffer);

// gl renders "upside down" so swap top to bottom into new array.
// there's gotta be a better way, but this works.
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
for(int y = 0; y <1024; y++)
{
for(int x = 0; x <768 * 4; x++)
{
buffer2[(1023 - y) * 768 * 4 + x] = buffer[y * 4 * 768 + x];
}
}

// make data provider with data.
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);

// prep the ingredients
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * 768;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

// make the cgimage
CGImageRef imageRef = CGImageCreate(768, 1024, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);

// then make the uiimage from that
UIImage *myImage = [UIImage imageWithCGImage:imageRef];

//free(buffer);
//free(buffer2);

return myImage;
}

注意最后那两个调用 free(buffer) 和 free(buffer2) 的地方了吗?这些在 iPad 模拟器上运行良好,消除了内存问题并允许我大胆地生成。但是,它们会立即杀死 iPad。就像,它第一次执行它。如果我删除 free() 调用,它运行良好,只是在一两分钟后内存不足。那么为什么 free() 调用会导致设备崩溃?

注意 - 不是调用 free() 导致设备崩溃,而是稍后崩溃。但这似乎是根本原因/..

编辑 - 有人问它究竟在哪里崩溃。此流程继续将图像返回给另一个对象,该对象将其写入文件。调用“UIImageJPEGRepresentation”方法时,它会生成一条 EXT_BAD_ACCESS 消息。我认为这是因为我传递给它以写入文件的 UIImage 已损坏、为空或其他内容。但这只会在我释放这两个缓冲区时发生。

如果内存以某种方式与 UIIMage 相关,我会理解,但实际上不应该,尤其是当它在模拟器上运行时。我想知道这是否取决于 iPad 如何处理“免费”电话...

最佳答案

来自阅读docs ,我相信 CGDataProviderCreateWithData 只会引用 buffer2 指向的内存,而不是复制它。在图像发布之前,您应该一直分配它。

试试这个:

static void _glToUIImageRelease (void *info, const void *data, size_t size) {    free(data);}-(UIImage *) glToUIImage {    NSInteger myDataLength = 768 * 1024 * 4;    // allocate array and read pixels into it.    GLubyte *buffer = (GLubyte *) malloc(myDataLength);    glReadPixels(0, 0, 768, 1024, GL_RGBA, GL_UNSIGNED_BYTE, buffer);    // gl renders "upside down" so swap top to bottom into new array.    // there's gotta be a better way, but this works.    GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);    for(int y = 0; y 

关于iphone - free() 调用在模拟器上有效,使 iPad 生气。 iPad粉碎,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3317723/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com