奇怪的dispatch_async内存行为

时间:2011-10-02 15:52:42

标签: objective-c ios multithreading grand-central-dispatch

我有以下dispatch_async代码:

dispatch_async(openGLESContextQueue, ^{

        [(EAGLView *)self.view setFramebuffer];

        // Replace the implementation of this method to do your own custom drawing.
        static const GLfloat squareVertices[] = {
            -0.5f, -0.33f,
            0.5f, -0.33f,
            -0.5f,  0.33f,
            0.5f,  0.33f,
        };

        static const GLubyte squareColors[] = {
            127, 127,   0, 127,
            0,   255, 255, 255,
            0,     0,   0,   0,
            255,   0, 255, 255,
        };

        static float transY = 0.0f;

        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);            

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
        transY += 0.075f;

        glVertexPointer(2, GL_FLOAT, 0, squareVertices);
        glEnableClientState(GL_VERTEX_ARRAY);
        glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
        glEnableClientState(GL_COLOR_ARRAY);

        glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

        [(EAGLView *)self.view presentFramebuffer];

    });

在使用Instruments时,即使动画运行正常,我也会得到大量“永远无法释放的64字节malloc”。谁知道为什么?

1 个答案:

答案 0 :(得分:1)

我终于能够使用信号量解决问题了:

if (dispatch_semaphore_wait(frameRenderingSemaphore, DISPATCH_TIME_NOW) == 0)
    {
        dispatch_async(openGLESContextQueue, ^{

            [(EAGLView *)self.view setFramebuffer];

            glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glMatrixMode(GL_MODELVIEW);
            glLoadIdentity();
            glTranslatef(0.0f, (GLfloat)(sinf(transY)/2.0f), 0.0f);
            transY += 0.075f;

            glVertexPointer(2, GL_FLOAT, 0, squareVertices);
            glEnableClientState(GL_VERTEX_ARRAY);
            glColorPointer(4, GL_UNSIGNED_BYTE, 0, squareColors);
            glEnableClientState(GL_COLOR_ARRAY);

            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

            [(EAGLView *)self.view presentFramebuffer];

            dispatch_semaphore_signal(frameRenderingSemaphore);
        });
    }

我猜调度队列没有时间处理洪水,无法处理每个opengl重绘。这样,它将一次只异步处理一次重绘。奇怪的是,它对帧速率没有任何副作用! :d

谢谢:)