简体   繁体   English

设备上令人沮丧的EXC_BAD_ACCESS(与NSData / OpenGL有关?)-在模拟器上工作正常

[英]Frustrating EXC_BAD_ACCESS (NSData / OpenGL related?) on device - works fine on simulator

This one is driving me mad. 这使我发疯。 I recently introduced some code that stores image data from an OpenGL view in a NSData object, and stores that in Core Data. 我最近介绍了一些代码,这些代码将来自OpenGL视图的图像数据存储在NSData对象中,并将其存储在Core Data中。 On retrieving this, and attempting to draw this pixel data back to a texture, I am receiving an EXC_BAD_ACCESS error. 检索此图像并尝试将此像素数据绘制回纹理时,我收到了EXC_BAD_ACCESS错误。

Here is the code that causes the bug: 这是导致该错误的代码:

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

What is making this extremely frustrating for me is that everything works absolutely fine on the simulator. 让我感到非常沮丧的是,模拟器上的所有功能都正常运行。 Also, I am fairly confident that this exact code did actually work on the device a few hours earlier... perhaps I am going mad! 另外,我非常有信心这个确切的代码实际上早几个小时就可以在设备上运行了……也许我发疯了!

I have tried the following with no luck: 我没有运气就尝试了以下方法:

  • Checked that the pixelData array is big enough to store all the bytes from the NSData object. 检查pixelData数组是否足够大以存储NSData对象中的所有字节。
  • Restarted the device, restarted Xcode (version 4 by the way), done a clean build, cleared the Core Data model and rebuilt it. 重新启动设备,重新启动Xcode(顺便说一下版本4),完成一个干净的构建,清除Core Data模型并重新构建它。
  • Ensured that the NSData object is properly retained when it is called. 确保在调用NSData对象时正确保留了该对象。
  • Gradually commented out lines of 逐渐注释掉
    code. 码。 For each line I comment out, 对于我注释掉的每一行,
    the EXC_BAD_ACCESS error moves EXC_BAD_ACCESS错误移动
    further down. 再向下。 Does this mean that the error could be the result of 这是否表示该错误可能是由于
    something returning from another 从另一个回来的东西
    thread? 线?

This one is really baffling me. 这真的让我感到困惑。 Most annoying is why everything works fine in the simulator. 最令人讨厌的是为什么在模拟器中一切正常。 I would greatly appreciate any help on this one! 对此,我将不胜感激!

EDIT 编辑

Here is the backtrace from the error: 这是错误的回溯:

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

Thanks 谢谢

You allocate pixelData on the stack. 您在堆栈上分配pixelData It's likely that you overflow the device's stack, which is probably smaller than what the simulator allows. 您可能溢出了设备的堆栈,该堆栈可能比模拟器允许的堆栈小。 I suggest allocating it on the heap using new instead. 我建议改为使用new在堆上分配它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM