仅通过从程序中删除 std::cout<<"hi"<<std::endl OpenGL 崩溃

OpenGL crash just by removing std::cout<<"hi"<<std::endl from the program

我发现了一个非常神秘的问题,我在 out 我在程序中的调试行和程序 "segmentaion fault (core dumped)"。

我缩小了程序范围,来到了这里。这是重现问题的完整代码:

#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <highgui.h>
#include <GL/glut.h>
#include <iostream>

int main(int argc, char **argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA | GLUT_ALPHA | GLUT_DEPTH | GLUT_DOUBLE);
  glutInitWindowSize(500, 281);
  (void) glutCreateWindow("Alpha Test");

  cv::Mat image = cv::imread("alphatest.png");
  int texWidth = image.cols;
  int texHeight = image.rows;
  GLuint texId;
  glGenTextures(1, &texId);
  glBindTexture(GL_TEXTURE_2D, texId);

  //std::cout << "hi" << std::endl;

  glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, image.ptr());
}

此程序在 glTexImage2D 崩溃并显示错误消息:

zsh: segmentation fault (core dumped)  ./mystery

(节目已取名为mystery)

通过取消对 cout 行的注释,程序可以正常运行而不会出现错误以及消息 "hi"。我想知道为什么我必须保留调试行?

这是alphatest.png

很可能您的图像行对齐不符合 OpenGL 读取图像的默认设置(4 字节行对齐)。这可能会导致 OpenGL 从未映射的页面读取,从而导致段错误。在执行可能创建页面分配的 iostream 输出时,与图像缓冲区相邻,因此此问题仍然存在 "hidden"。

测试一下:

glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);  /* tightly packed*/
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0); /* tightly packed*/
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);   /* tightly packed*/
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);   /* tightly aligned*/
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_BGRA, GL_UNSIGNED_BYTE, image.ptr());

此外,如果您的图像是灰度图像,那么读取 GL_BGRA 格式将需要比 imread 传送更多的数据。您不应对这些参数进行硬编码,而应从图像文件中将它们确定为 imread returns.