OpenGL绘制模型时触发Segmentation Fault问题求助
Hey there, let’s dig into your OpenGL segmentation fault issue on Ubuntu 16.04 — this looks like a classic case of malformed generated data or code inconsistencies, so let’s break it down step by step.
Problem Recap
- Your OpenGL app runs fine when models like
zewnetrznydach3orsciany5are commented out - Uncommenting any of these models triggers a Segmentation Fault (core dumped)
- All models are auto-generated via Python scripts (only vertex/attribute data differs)
- Updating your graphics driver didn’t fix the issue
Working Model Example (zewnetrznyDach2)
This code runs without issues:
#include "zewnetrznydach2.h" namespace Models { ZewnetrznyDach2 zewnetrznydach2; ZewnetrznyDach2::ZewnetrznyDach2() { vertices=ZewnetrznyDach2Internal::vertices; normals=ZewnetrznyDach2Internal::normals; vertexNormals=ZewnetrznyDach2Internal::vertexNormals; texCoords=ZewnetrznyDach2Internal::texCoords; colors=ZewnetrznyDach2Internal::colors; vertexCount=ZewnetrznyDach2Internal::vertexCount; } ZewnetrznyDach2::~ZewnetrznyDach2() { } void ZewnetrznyDach2::drawSolid() { glEnable(GL_NORMALIZE); glEnableClientState(GL_VERTEX_ARRAY); //glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(4,GL_FLOAT,0,vertices); //glColorPointer(4,GL_FLOAT,0,colors); glNormalPointer(GL_FLOAT,sizeof(float)*4,vertexNormals); glTexCoordPointer(2,GL_FLOAT,0,texCoords); glDrawArrays(GL_TRIANGLES,0,vertexCount); glDisableClientState(GL_VERTEX_ARRAY); //glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } namespace ZewnetrznyDach2Internal { unsigned int vertexCount=36; float vertices[]={ -16.193,-2.25,-9.829,1.0, -15.999,-2.099,-9.829,1.0, -5.413,-2.25,-9.829,1.0, -15.999,-2.099,-9.829,1.0, -16.193,-2.25,-9.829,1.0, -16.193,6.2,-9.829,1.0, -15.999,-2.099,-9.829,1.0, -16.193,6.2,-9.829,1.0, -15.999,5.820,-9.829,1.0, -15.999,5.820,-9.829,1.0, -16.193,6.2,-9.829,1.0, -5.639,5.820,-9.829,1.0, -5.413,6.2,-9.829,1.0, -5.639,5.820,-9.829,1.0, -16.193,6.2,-9.829,1.0, -5.413,6.2,-9.829,1.0, -5.639,-2.099,-9.829,1.0, -5.639,5.820,-9.829,1.0, -5.413,-2.25,-9.829,1.0, -5.639,-2.099,-9.829,1.0, -5.413,6.2,-9.829,1.0, -5.639,-2.099,-9.829,1.0, -5.413,-2.25,-9.829,1.0, -15.999,-2.099,-9.829,1.0, -5.639,-2.099,-9.829,1.0, -15.999,-2.099,-9.829,1.0, -15.999,-2.099,-9.769,1.0, -15.999,-2.099,-9.829,1.0, -15.999,5.820,-9.829,1.0, -15.999,5.820,-9.769,1.0, -15.999,5.820,-9.829,1.0, -5.639,5.820,-9.829,1.0, -5.639,5.820,-9.769,1.0, -5.639,5.820,-9.829,1.0, -5.639,-2.099,-9.829,1.0, -5.639,-2.099,-9.769,1.0, }; float colors[]={ }; float normals[]={ 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,0,-1,0, 0,1,-0,0, 0,1,-0,0, 0,1,-0,0, 1,0,-0,0, 1,0,-0,0, 1,0,-0,0, 0,-1,-0,0, 0,-1,-0,0, 0,-1,-0,0, -1,0,-0,0, -1,0,-0,0, -1,0,-0,0, }; float vertexNormals[]={ }; float texCoords[]={ 637.526,-88.582, 629.904,-82.667, 213.116,-88.582, 629.904,-82.667, 637.526,-88.582, 637.526,244.094, 629.904,-82.667, 637.526,244.094, 629.904,229.143, 629.904,229.143, 637.526,244.094, 222.03,229.143, 213.116,244.094, 222.03,229.143, 637.526,244.094, 213.116,244.094, 222.03,-82.667, 222.03,229.143, 213.116,-88.582, 222.03,-82.667, 213.116,244.094, 222.03,-82.667, 213.116,-88.582, 629.904,-82.667, -222.03,386.989, -629.904,386.989, -629.904,384.626, 386.989,-82.667, 386.989,229.143, 384.626,229.143, 629.904,386.989, 222.03,386.989, 222.03,384.626, -386.989,229.143, -386.989,-82.667, -384.626,-82.667, }; } }
Non-Working Model Example (zewnetrznydach3)
The code you provided is truncated — notice the drawSolid() function cuts off mid-line:
#include "zewnetrznydach3.h" namespace Models { ZewnetrznyDach3 zewnetrznydach3; ZewnetrznyDach3::ZewnetrznyDach3() { vertices=ZewnetrznyDach3Internal::vertices; normals=ZewnetrznyDach3Internal::normals; vertexNormals=ZewnetrznyDach3Internal::vertexNormals; texCoords=ZewnetrznyDach3Internal::texCoords; colors=ZewnetrznyDach3Internal::colors; vertexCount=ZewnetrznyDach3Internal::vertexCount; } ZewnetrznyDach3::~ZewnetrznyDach3() { } void ZewnetrznyDach3::drawSolid() { glEnable(GL_NORMALIZE); glEnableClientState(GL_VERTEX_ARRAY); //glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glEnableClientState(
Likely Causes & Fixes
1. Corrupted/Incomplete Generated Code
First, fix the truncated code for zewnetrznydach3 — incomplete functions will cause compilation errors or undefined behavior (like segfaults). Even if you have a full version, check for:
- Missing closing braces
}in functions or namespaces - Mismatched OpenGL function arguments (e.g., passing a null pointer to
glVertexPointer) - Typos in OpenGL enum names (though this usually causes compilation errors, not segfaults)
2. Mismatched Attribute Data Lengths
Since all models are Python-generated, the most probable issue is inconsistent data between vertexCount and your attribute arrays:
- For your working model,
vertexCount=36, andverticeshas36 * 4 = 144floats (correct, since each vertex is 4 floats). If your problematic models have avertexCountthat doesn’t match the actual number of vertices invertices,glDrawArrayswill read past the end of the array, causing a segfault. - Check other attributes too:
texCoordsshould havevertexCount * 2floats,normalsshould align with the number of faces/vertices, etc. - Add validation to your Python script to ensure these lengths match before generating the C++ code.
3. Empty Attribute Arrays Enabled in OpenGL
In your working code, you enable GL_NORMAL_ARRAY but vertexNormals is an empty array. While this might work for some drivers, it’s undefined behavior. For problematic models, try commenting out glEnableClientState(GL_NORMAL_ARRAY) and glNormalPointer to see if that fixes the segfault — if it does, your Python script isn’t generating valid normal data for those models.
4. Stack Overflow from Large Arrays
You’re declaring large float arrays (like vertices) on the stack. If your problematic models are much larger than zewnetrznydach2, this could cause a stack overflow. Fix this by:
- Moving the arrays to the heap (using
new float[]orstd::vector<float>) - Increasing the stack size (less ideal, as it’s system-dependent)
5. Debugging to Pinpoint the Crash
Use gdb to find exactly where the segfault happens:
- Compile your program with debug symbols (
-gflag in GCC) - Run
gdb ./your_app - Type
runto start the program - When it crashes, type
btto get a backtrace — this will show you which line (in your code or OpenGL driver) is causing the crash.
内容的提问来源于stack exchange,提问作者Deloryn

