使用openGL和OOP的程序中的Valgrind错误

时间:2018-11-22 18:33:04

标签: c++ opengl valgrind

我现在学习OpenGL,并为标准过程(如初始化glfw,创建着色器,加载模型等)编写了一个简单的oop-wrapping。我将Ubuntu 16.04与g ++ 5.4.0结合使用。当我使用valgrind --tool = memchek检查程序时,它显示了很多错误(30K +),例如

   "Invalid write/read of size 4" at ...by 0x50CC471: ??? (in 
    /usr/lib/x86_64-linux-gnu/libglfw.so.3.1)
    ==14520==    by 0x50C9A06: ??? (in /usr/lib/x86_64-linux- 
    gnu/libglfw.so.3.1)
    ==14520==    by 0x50C4B69: glfwCreateWindow (in /usr/lib/x86_64-linux- 
    gnu/libglfw.so.3.1)
    ==14520==    by 0x403EC1: Engine::Engine() (in 
    /home/paul/programming/openGL/main)

当我在gdb中远程使用valgrind时显示了此特定错误,这是到达第一个断点后的错误,在那里调用了Engine类构造函数。即使发生此类错误。为什么? 我读过,这样的错误可能是未初始化变量的结果,但是为什么堆栈跟踪中有glfw库?他们有这样的错误吗? Engine类构造函数的代码为:

  // setEventHandling();
    //default values for width and height
    window_width = 1280;
    window_height = 720;
    //default shader filenames
    int init_result = glfwInit();
    if (!init_result)
    {
        cout << "Failed to initialize GLFW" << endl;
    }
    //major required version of the opengl
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    //setting of the profile for which context is created
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    window = glfwCreateWindow(window_width, window_height, "OpenGL", NULL, 
    NULL);

    if (window == NULL)
    {
        cout << "Failed to create window" << endl;
        glfwTerminate();
    }
    glfwMakeContextCurrent(window);
    glewExperimental = true;
    if (glewInit() != GLEW_OK)
    {
        cout << "Failed to initialize GLEW" << endl;
    }
    glfwGetFramebufferSize(window, &window_width, &window_height);
    glViewport(0, 0, window_width, window_height);
    glfwSetKeyCallback(this->window, 
    CallbackInterface::callback_dispatch);

我在这里看不到任何未初始化的变量。那么为什么我看到这个错误? 顺便说一下,我早些时候使用SFML编码图形,并且错误是相同的。

编辑

评论中的某些人告诉我们,问题实际上是因为我使用OOP,因此在OpenGL中使用它是很不好的。好吧,我测试了最简单的openGL代码,该代码仅创建一个窗口...

// GLEW
#define GLEW_STATIC  
#include <GL/glew.h>

// GLFW
#include <GLFW/glfw3.h>


// Function prototypes
void key_callback(GLFWwindow* window, int key, int scancode, int 
action, int mode);

// Window dimensions
const GLuint WIDTH = 800, HEIGHT = 600;

// The MAIN function, from here we start the application and run the 
game loop
int main()
{
std::cout << "Starting GLFW context, OpenGL 3.3" << std::endl;
// Init GLFW
glfwInit();
// Set all the required options for GLFW
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

// Create a GLFWwindow object that we can use for GLFW's functions
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", 
nullptr, nullptr);    
if (window == nullptr)
{
    std::cout << "Failed to create GLFW window" << std::endl;
    glfwTerminate();
    return -1;
}
glfwMakeContextCurrent(window);
// Set the required callback functions
glfwSetKeyCallback(window, key_callback);

// Set this to true so GLEW knows to use a modern approach to 
retrieving function pointers and extensions
glewExperimental = GL_TRUE;
// Initialize GLEW to setup the OpenGL Function pointers
if (glewInit() != GLEW_OK)
{
    std::cout << "Failed to initialize GLEW" << std::endl;
    return -1;
}    

// Define the viewport dimensions
int width, height;
glfwGetFramebufferSize(window, &width, &height);  
glViewport(0, 0, width, height);

// Game loop
while (!glfwWindowShouldClose(window))
{
    // Check if any events have been activiated (key pressed, mouse 
moved etc.) and call corresponding response functions
    glfwPollEvents();

    // Render
    // Clear the colorbuffer
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    // Swap the screen buffers
    glfwSwapBuffers(window);
}

// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}

// Is called whenever a key is pressed/released via GLFW
void key_callback(GLFWwindow* window, int key, int scancode, int 
action, int mode)
{
std::cout << key << std::endl;
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
    glfwSetWindowShouldClose(window, GL_TRUE);
}

Valgrind输出为26K错误!其中大多数是大小为4/8的无效读/写

==11533== HEAP SUMMARY:
==11533==     in use at exit: 114,489 bytes in 669 blocks
 ==11533==   total heap usage: 12,989 allocs, 12,320 frees, 3,704,593 
bytes 
allocated
==11533== 
==11533== LEAK SUMMARY:
==11533==    definitely lost: 72 bytes in 1 blocks
==11533==    indirectly lost: 0 bytes in 0 blocks
==11533==      possibly lost: 0 bytes in 0 blocks
==11533==    still reachable: 114,417 bytes in 668 blocks
==11533==         suppressed: 0 bytes in 0 blocks
==11533== Rerun with --leak-check=full to see details of leaked memory
==11533== 
==11533== For counts of detected and suppressed errors, rerun with: -v
==11533== ERROR SUMMARY: 26274 errors from 478 contexts (suppressed: 0 
from 0)

这让我认为glfw和opengl本身存在很多错误。

0 个答案:

没有答案