绘制太多元素时glDrawArrays()会出现段错误

时间:2011-06-12 22:26:06

标签: android opengl opengl-es android-ndk

我在C ++中有一些简单的OpenGL ES代码,我在运行Android的手机,运行webOS的手机和使用SDL / OpenGL的Mac上运行。有问题的代码只会绘制一个存储在GL_LINE_STRIP数组中的GLfloat。以下是我尝试这样做的方式(版本1 ):

GLfloat array* = new GLfloat[2048];
//later... (array gets filled)
int howMany = 1024;
glVertexPointer(2, GL_FLOAT, 0, array); checkError();
glDrawArrays(GL_LINE_STRIP, 0, howMany); checkError();

似乎它应该非常简单。但是,上面的代码段落在Android和我的Mac上的OpenGL驱动程序中(无论出于何种原因,似乎在webOS上运行正常。)这是我要做的就是避免这种情况(版本2 ):< / p>

GLfloat array* = new GLfloat[2048];
//later... (array gets filled)
int howMany = 1024;
for (unsigned i = 0; i < howMany; i += 789) {
    glVertexPointer(2, GL_FLOAT, 0, array+i*2); checkError();
    //this is not a bug: we draw one more than we increment by, or there is a hole in the graph
    glDrawArrays(GL_LINE_STRIP, 0, std::min(790U, howMany-i)); checkError();
}

限制一次绘制的行数可以避免段错误。数字790是通过实验确定的;在791它在OSX上崩溃(Android可以更高,虽然它曾经被限制在113多个版本之前...所以这个数字似乎对于每个应用程序版本是一致的,但不是在不同版本或平台之间。)

我的应用程序中还有很多其他GL内容,但它是一个大型应用程序并且发布所有代码都不实用。那么,有什么可能导致这种情况呢?我应该考虑哪些可能性?

这是我尝试使用版本1时在OSX上运行和崩溃的程序的valgrind输出:

==85414== Conditional jump or move depends on uninitialised value(s)
==85414==    at 0x1D022993: glVertexPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==    by 0x6AA59A9: glVertexPointer (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib)
==85414==    by 0x2B001: draw_elements (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x2B44C: glDrawArrays (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0xB639: GLImage::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0xC292: GLLabel::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x1AC67: RTAPlot::drawAxes(int) (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x1B30A: RTAPlot::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x225F5: RTAPlotView::updateForeground() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x19FC4: RTAComponent::update() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x805D: AudiaApplication_private::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x267DF: AudiaApplication::run() (in ./rta.app/Contents/MacOS/rta)
==85414==  Uninitialised value was created by a stack allocation
==85414==    at 0x1D02268F: glVertexPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== 
==85414== Conditional jump or move depends on uninitialised value(s)
==85414==    at 0x1D022E0C: glTexCoordPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==    by 0x6AA5216: glTexCoordPointer (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib)
==85414==    by 0x2B279: draw_elements (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x2B44C: glDrawArrays (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0xB639: GLImage::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0xC292: GLLabel::glDraw(int, int) (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x1AC67: RTAPlot::drawAxes(int) (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x1B30A: RTAPlot::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x225F5: RTAPlotView::updateForeground() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x19FC4: RTAComponent::update() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x805D: AudiaApplication_private::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x267DF: AudiaApplication::run() (in ./rta.app/Contents/MacOS/rta)
==85414==  Uninitialised value was created by a stack allocation
==85414==    at 0x1D022A6B: glTexCoordPointer_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414== 
==85414== Invalid read of size 8
==85414==    at 0x1D0B9166: gleRunVertexSubmitImmediate (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==    by 0x1D0B84FB: gleLLVMArrayFunc (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==    by 0x1D0A14F2: gleDrawArraysOrElements_ExecCore (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==    by 0x1D0A2A04: glDrawElements_IMM_Exec (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Resources/GLEngine.bundle/GLEngine)
==85414==    by 0x6AA304F: glDrawElements (in /System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib)
==85414==    by 0x2B2DB: draw_elements (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x2B44C: glDrawArrays (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x1FEF6: RTAPlotCanvas::drawLineStrip(float*, unsigned int) (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x1E91A: RTAPlotCanvas::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x1B318: RTAPlot::updateGL() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x225F5: RTAPlotView::updateForeground() (in ./rta.app/Contents/MacOS/rta)
==85414==    by 0x19FC4: RTAComponent::update() (in ./rta.app/Contents/MacOS/rta)
==85414==  Address 0xc0000000 is not stack'd, malloc'd or (recently) free'd
==85414== 
==85414== 
==85414== Process terminating with default action of signal 11 (SIGSEGV)
==85414==  General Protection Fault
==85414==    at 0x10AED0: misaligned_stack_error_ (in /usr/lib/libSystem.B.dylib)
==85414==    by 0x1E45E87: -[NSCustomReleaseData dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414==    by 0x1E45E37: -[NSBitmapImageRep _freeData] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414==    by 0x1E45DCA: -[NSBitmapImageRep _freeImage] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414==    by 0x1E45D74: -[NSBitmapImageRep dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414==    by 0x171DF37: CFRelease (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
==85414==    by 0x1748691: __CFArrayReleaseValues (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
==85414==    by 0x171E100: _CFRelease (in /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation)
==85414==    by 0x1D89F7A: -[NSImage dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414==    by 0x20857D4: -[NSCursor dealloc] (in /System/Library/Frameworks/AppKit.framework/Versions/C/AppKit)
==85414==    by 0xDFE33: QZ_FreeWMCursor (in /opt/PalmPDK/host/lib/libSDL-1.2.0.11.2.dylib)
==85414==    by 0xCA11F: SDL_FreeCursor (in /opt/PalmPDK/host/lib/libSDL-1.2.0.11.2.dylib)
--85414:0:schedule VG_(sema_down): read returned -4

请注意,前两个条件跳转错误每次都会发生,并且可能不是我的应用程序的问题。只有在我尝试在OSX上使用版本1时才会出现无效读取错误。

1 个答案:

答案 0 :(得分:7)

它可能与您的顶点数组无关。例如,可能仍有指向其他顶点属性集的指针,当然如果它们的缓冲区比新顶点数组小,则会出现段错误。

如果仔细观察你的valgrind输出,你会看到,仍然有一个纹理坐标指针设置。

所以我建议您禁用所有不需要的顶点属性数组:

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
/* ... */

在你的情况下

glDisableClientState(GL_TEXTURE_COORD_ARRAY);