2014-02-12 50 views
0

Valgrind給我一個來自紋理加載方法的內存泄漏報告。我已經追溯到一種方法,但它似乎是FreeImage庫本身的泄漏?FreeImage_Load內存泄漏

(內存泄漏的其餘部分都來自FreeImage_Load過,此處省略)

==4295== 4,320,746 (8 direct, 4,320,738 indirect) bytes in 1 blocks are definitely lost in loss record 210 of 210 
==4295== at 0x4C2CD7B: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) 
==4295== by 0x85148C: FreeImage_AllocateHeaderT (in /tmp/lab) 
==4295== by 0x851913: FreeImage_AllocateHeader (in /tmp/lab) 
==4295== by 0x8698AE: Load(FreeImageIO*, void*, int, int, void*) (in /tmp/lab) 
==4295== by 0x855B44: FreeImage_LoadFromHandle (in /tmp/lab) 
==4295== by 0x855BE0: FreeImage_Load (in /tmp/lab) 
==4295== by 0x41433F: Texture::LoadTexture(std::string, float, float) (Texture.cpp:7) 
==4295== by 0x417E2B: World::LoadTextures() (World.cpp:27) 
==4295== by 0x4119E5: main (project_main.cpp:217) 
==4295== 
==4295== LEAK SUMMARY: 
==4295== definitely lost: 12,328 bytes in 17 blocks 
==4295== indirectly lost: 15,095,237 bytes in 554 blocks 
==4295==  possibly lost: 845,677 bytes in 93 blocks 
==4295== still reachable: 47,631 bytes in 425 blocks 
==4295==   suppressed: 0 bytes in 0 blocks 

這似乎表明,泄漏發生在紋理:: LoadTexture方法,這裏是方法:

GLuint Texture::LoadTexture(std::string filename, float width, float height){ 

    //Load Image 
    FREE_IMAGE_FORMAT format = FreeImage_GetFileType(filename.c_str(), 0);  
    FIBITMAP *image = FreeImage_ConvertTo32Bits(FreeImage_Load(format, filename.c_str())); 
    BYTE *bits = FreeImage_GetBits(image); 

    //Get width, height, bitnumber 
    int w = FreeImage_GetWidth(image); 
    int h = FreeImage_GetHeight(image); 
    int nBPP = FreeImage_GetBPP(image); 

    std::cout<<filename<<" "<<w<<"*"<<h<< " " << nBPP << std::endl; 

    //Generate texture references 
    GLuint textureID; 
    glGenTextures(1, &textureID); 
    //Bind 
    glBindTexture(GL_TEXTURE_2D, textureID); 
    //Set parameters 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);  
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 

    //Generate actual texture from image data (mipmaps) 
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, w, h, GL_BGRA, GL_UNSIGNED_BYTE, bits); 

    //Unload image 
    FreeImage_Unload(image);    

    //Add texture info to a map 
    textures.insert(std::pair<GLuint, TextureInfo>(textureID, TextureInfo(textureID, w, h, nBPP)));  

    if (glGetError()) { 
     std::cout << "There was an error loading the texture" << std::endl; 
    } 

    //Return OpenGL textureID 
    return textureID; 
} 

任何人都可以幫助表示讚賞,不知道如何去調試這一個。

回答

0

我看到一個問題,此行:

FIBITMAP *image = FreeImage_ConvertTo32Bits(FreeImage_Load(format, filename.c_str())); 

FreeImage_Load()調用返回FIBITMAP *,你必須通過調用FreeImage_Unload()釋放。在您的代碼中,您將返回的指針視爲臨時值並將其傳遞給FreeImage_ConvertTo32Bits(),但不會將其保存在任何地方。你泄漏內存是因爲你丟失了指針並且不能再釋放資源。

注意FreeImage_ConvertTo32Bits()使位圖的克隆所以你必須調用FreeImage_Unload()除了釋放到原來的FIBITMAP *

+0

修好了,謝謝! – rilott