这个让我发疯。我最近介绍了一些代码,用于将来自OpenGL视图的图像数据存储在NSData对象中,并将其存储在Core Data中。在检索此项并尝试将此像素数据绘制回纹理时,我收到EXC_BAD_ACCESS错误。
以下是导致错误的代码:
- (void)drawDrawingFromData:(NSData *)data {
if (data != nil) {
[renderer prepareRetainedBuffer];
/* Prepare data in raw bytes */
NSInteger width = self.bounds.size.width, height = self.bounds.size.height;
NSUInteger dataLength = width * height * 4;
unsigned char pixelData[dataLength];
[data getBytes:pixelData length:dataLength]; // <---- EXC_BAD_ACCESS here.
/* Create a texture to render using pixel data */
GLuint imageTexture;
glGenTextures(1, &imageTexture); // <---- EXC_BAD_ACCESS occurs here if the [data getBytes:length:] line is commented out.
glBindTexture(GL_TEXTURE_2D, imageTexture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// Destination coords.
GLfloat retainedVertices[] = {
0.0, 0.0,
width, 0.0,
0.0, height,
width, height,
};
// Source coords.
GLfloat retainedTexCoords[] = {
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0
};
glColor4f(1.0, 1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, imageTexture);
glVertexPointer(2, GL_FLOAT, 0, retainedVertices);
glTexCoordPointer(2, GL_FLOAT, 0, retainedTexCoords);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
[renderer renderSceneWithScratch:NO];
[renderer presentScene];
}
}
让我感到非常沮丧的是,在模拟器上一切正常。此外,我相当确信这个确切的代码确实在几小时前在设备上工作了......也许我会发疯了!
我试过以下但没有运气:
这个让我很困惑。最烦人的是为什么在模拟器中一切正常。我非常感谢对此提供任何帮助!
修改
以下是错误的回溯:
#0 0x36cc6c92 in objc_msgSend ()
#1 0x0001e71c in -[EAGLView drawDrawingFromData:] (self=0x14dbf0, _cmd=0x4e16f, data=0x13dbd0) at /Users/Stu/Documents/...
#2 0x0001df30 in -[EAGLView layoutSubviews] (self=0x14dbf0, _cmd=0x329644ac) at /Users/Stu/Documents/...
#3 0x326675fa in -[UIView(CALayerDelegate) layoutSublayersOfLayer:] ()
#4 0x35ee2f02 in -[NSObject(NSObject) performSelector:withObject:] ()
#5 0x333e2bb4 in -[CALayer layoutSublayers] ()
#6 0x333e296c in CALayerLayoutIfNeeded ()
#7 0x333e81c4 in CA::Context::commit_transaction ()
#8 0x333e7fd6 in CA::Transaction::commit ()
#9 0x333e1054 in CA::Transaction::observer_callback ()
#10 0x35f4ca34 in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ ()
#11 0x35f4e464 in __CFRunLoopDoObservers ()
#12 0x35f4f75a in __CFRunLoopRun ()
#13 0x35edfec2 in CFRunLoopRunSpecific ()
#14 0x35edfdca in CFRunLoopRunInMode ()
#15 0x3253c41e in GSEventRunModal ()
#16 0x3253c4ca in GSEventRun ()
#17 0x32690d68 in -[UIApplication _run] ()
#18 0x3268e806 in UIApplicationMain ()
#19 0x000023a0 in main (argc=1, argv=0x2fdff540) at /Users/Stu/Documents/...
由于
答案 0 :(得分:1)
您在堆栈上分配pixelData
。您可能会溢出设备的堆栈,这可能比模拟器允许的堆栈小。我建议使用new
在堆上分配它。