告诉valgrind实际初始化了一个值

时间:2015-09-27 17:55:50

标签: c++ opengl virtual-machine valgrind

我对valgrind的误报告诉我,条件跳转或移动取决于未初始化的值。

代码是这样的:

//from GenerateTexture:
glGenTextures(1, (GLuint*)&texture_list[texture_pos]);
return texture_list[texture_pos++];

// Usage in another function
if(texture == 0)
    InitTexture(); // First line of function: texture = GenerateTexture();
if(texture == 0) // This is what valgrind complains about
    return;

我在使用Windows主机的Ubuntu VM中运行代码时有一些关于OGL库的早期警告,因此我假设valgrind无法正确处理VM。但是我确实知道纹理是正确初始化的,并且不希望每次使用texture时都被valgrind打扰。

我如何告诉valgrind(可能通过代码注释)这是误报?我知道抑制文件,但是我需要添加每个使用纹理的函数,所以我正在寻找一个更清洁的"替代品。

编辑:请求MCVE:

#include <GL/gl.h>
#include <SDL/SDL.h>
#include <iostream>

int main(int argc, char** argv){
    SDL_Init(SDL_INIT_VIDEO);

    const SDL_VideoInfo* video = SDL_GetVideoInfo( );

    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
    SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

    SDL_SetVideoMode( 800, 600, video->vfmt->BitsPerPixel, SDL_OPENGL );

    GLuint tex;
    glGenTextures(1, &tex);
    if(tex) std::cout << "1337";

    return 0;
}

VBox的Valgrind表示:

==2304== Conditional jump or move depends on uninitialised value(s)
==2304==    at 0xD97E21C: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94EB90: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0x400B49: main (test.cpp:46)
==2304==  Uninitialised value was created by a heap allocation
==2304==    at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2304==    by 0xD596F91: crAlloc (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD5A8688: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59840D: crNetConnectToServer (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59856D: crNetServerConnect (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD94C4F8: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94A07E: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94B0EC: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD2CAD09: crSPULoad (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD2CAEC3: crSPULoadChain (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD283FB6: ??? (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD284318: ??? (in /usr/lib/VBoxOGL.so)
==2304== 
==2304== Conditional jump or move depends on uninitialised value(s)
==2304==    at 0xD5939E4: crHashIdPoolAllocId (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD593AF7: crHashtableAllocRegisterKey (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD97E207: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94EB90: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0x400B49: main (test.cpp:46)
==2304==  Uninitialised value was created by a heap allocation
==2304==    at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2304==    by 0xD596F91: crAlloc (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD5A8688: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59840D: crNetConnectToServer (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59856D: crNetServerConnect (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD94C4F8: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94A07E: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94B0EC: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD2CAD09: crSPULoad (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD2CAEC3: crSPULoadChain (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD283FB6: ??? (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD284318: ??? (in /usr/lib/VBoxOGL.so)
==2304== 
==2304== Conditional jump or move depends on uninitialised value(s)
==2304==    at 0xD5939FC: crHashIdPoolAllocId (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD593AF7: crHashtableAllocRegisterKey (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD97E207: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94EB90: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0x400B49: main (test.cpp:46)
==2304==  Uninitialised value was created by a heap allocation
==2304==    at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2304==    by 0xD596F91: crAlloc (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD5A8688: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59840D: crNetConnectToServer (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59856D: crNetServerConnect (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD94C4F8: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94A07E: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94B0EC: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD2CAD09: crSPULoad (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD2CAEC3: crSPULoadChain (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD283FB6: ??? (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD284318: ??? (in /usr/lib/VBoxOGL.so)
==2304== 
==2304== Conditional jump or move depends on uninitialised value(s)
==2304==    at 0xD593A27: crHashIdPoolAllocId (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD593AF7: crHashtableAllocRegisterKey (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD97E207: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94EB90: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0x400B49: main (test.cpp:46)
==2304==  Uninitialised value was created by a heap allocation
==2304==    at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2304==    by 0xD596F91: crAlloc (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD5A8688: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59840D: crNetConnectToServer (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59856D: crNetServerConnect (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD94C4F8: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94A07E: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94B0EC: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD2CAD09: crSPULoad (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD2CAEC3: crSPULoadChain (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD283FB6: ??? (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD284318: ??? (in /usr/lib/VBoxOGL.so)
==2304== 
==2304== Conditional jump or move depends on uninitialised value(s)
==2304==    at 0xD593A29: crHashIdPoolAllocId (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD593AF7: crHashtableAllocRegisterKey (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD97E207: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94EB90: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0x400B49: main (test.cpp:46)
==2304==  Uninitialised value was created by a heap allocation
==2304==    at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2304==    by 0xD596F91: crAlloc (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD5A8688: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59840D: crNetConnectToServer (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59856D: crNetServerConnect (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD94C4F8: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94A07E: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94B0EC: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD2CAD09: crSPULoad (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD2CAEC3: crSPULoadChain (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD283FB6: ??? (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD284318: ??? (in /usr/lib/VBoxOGL.so)
==2304== 
==2304== Conditional jump or move depends on uninitialised value(s)
==2304==    at 0xD593A30: crHashIdPoolAllocId (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD593AF7: crHashtableAllocRegisterKey (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD97E207: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94EB90: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0x400B49: main (test.cpp:46)
==2304==  Uninitialised value was created by a heap allocation
==2304==    at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2304==    by 0xD596F91: crAlloc (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD5A8688: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59840D: crNetConnectToServer (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59856D: crNetServerConnect (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD94C4F8: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94A07E: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94B0EC: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD2CAD09: crSPULoad (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD2CAEC3: crSPULoadChain (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD283FB6: ??? (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD284318: ??? (in /usr/lib/VBoxOGL.so)
==2304== 
==2304== Conditional jump or move depends on uninitialised value(s)
==2304==    at 0x400B52: main (test.cpp:47)
==2304==  Uninitialised value was created by a heap allocation
==2304==    at 0x4C2BBA0: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2304==    by 0xD596F91: crAlloc (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD5A8688: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59840D: crNetConnectToServer (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD59856D: crNetServerConnect (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLcrutil.so)
==2304==    by 0xD94C4F8: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94A07E: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD94B0EC: ??? (in /opt/VBoxGuestAdditions-4.3.14/lib/VBoxOGLpackspu.so)
==2304==    by 0xD2CAD09: crSPULoad (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD2CAEC3: crSPULoadChain (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD283FB6: ??? (in /usr/lib/VBoxOGL.so)
==2304==    by 0xD284318: ??? (in /usr/lib/VBoxOGL.so)

1 个答案:

答案 0 :(得分:2)

您可以使用valgrind memcheck请求VALGRIND_MAKE_MEM_DEFINED。

参见http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.clientreqs 和文件memcheck.h了解更多信息。

但请注意,为变量赋值不是证据 初始化此变量:如果将值赋给 变量本身不是(完全)初始化,然后是'undefinedness' 将被传播。 因此,在声明定义的纹理变量之前,您应该这样做 仔细检查它是否有效地正确初始化/定义。

你可能会,例如首先运行--track-originins = yes以获得更多信息 关于未定义值的来源。

或者,您可以以交互方式搜索根本原因 使用valgrind + vgdb和gdb,检查例如 texture_list。

请参阅 http://www.valgrind.org/docs/manual/manual-core-adv.html#manual-core-adv.gdbserver了解更多信息。