Mi aplicación se está quedando sin memoria. Para resolver esto, libero dos matrices muy grandes usadas en una función que escribe un framebuffer en una imagen. El método es el siguiente:free() llamada funciona en el simulador, enoja al iPad. iPad smash
-(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;
}
Nota los dos llamados a libre (buffer) y libre (tampón 2) al final hay? Esos funcionan bien en el simulador de iPad, eliminando el problema de memoria y permitiéndome generar con imprudencia. Sin embargo, matan el iPad al instante. Al igual que la primera vez que lo ejecuta. Si elimino las llamadas gratuitas() funciona bien, simplemente se queda sin memoria después de uno o dos minutos. Entonces, ¿por qué la llamada gratuita() bloquea el dispositivo?
Nota: no es la llamada a free() que bloquea explícitamente el dispositivo, se bloquea más tarde. Pero esa parece ser la causa raíz/..
EDITAR - Alguien pregunta dónde se bloquea exactamente. Este flujo continúa para devolver la imagen a otro objeto, que lo escribe en un archivo. Al llamar al método 'UIImageJPEGRepresentation', genera un mensaje EXT_BAD_ACCESS. Supongo que esto es porque el UIImage que estoy pasando para escribir en el archivo está dañado, nulo o algo más. Pero esto solo sucede cuando libero esos dos buffers.
Entiendo si la memoria estaba de alguna manera relacionada con el UIIMage, pero realmente no debería ser así, especialmente porque funciona en el simulador. Me preguntaba si se debe a que iPad maneja las llamadas "gratuitas" ...
¡Acabo de sacar eso también! Excelente lugar, mi buen hombre. ¡Gracias! :) – mtrc