添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

In the following code snippet, I am leaking 52 bytes of memory for every iteration of the for loop, but after several hours I am unable to determine why =/

Any help will be appreciated.

    for(unsigned char i=0; i<128; ++i)
        ttf.loadGlyph(i);
        FT_Glyph glyph;
        FT_Get_Glyph(ttf.ftFace_->glyph, &glyph);
        FT_Glyph_To_Bitmap( &glyph, ft_render_mode_normal, 0, 1 );
        FT_BitmapGlyph bitmap_glyph = (FT_BitmapGlyph)glyph;
        FT_Bitmap& bitmap = bitmap_glyph->bitmap;
        int width = nextPowerOf2(bitmap.width);
        int height = nextPowerOf2(bitmap.rows);
        GLubyte* expanded_data = new GLubyte[ 2 * width * height]; // Allocate Memory For The Texture Data.
        for(int j=0; j <height;j++) {
            for(int i=0; i < width; i++){
                expanded_data[2*(i+j*width)]= expanded_data[2*(i+j*width)+1] =
                    (i>=bitmap.width || j>=bitmap.rows) ?
                    0 : bitmap.buffer[i + bitmap.width*j];
        glBindTexture( GL_TEXTURE_2D, fontData_.pTextures_[i]);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_LUMINANCE_ALPHA, GL_UNSIGNED_BYTE, expanded_data );
        delete [] expanded_data;
        glNewList(fontData_.fontBase_+i, GL_COMPILE);
        glBindTexture(GL_TEXTURE_2D, fontData_.pTextures_[i]);
        glPushMatrix();
        glTranslatef( (GLfloat)bitmap_glyph->left, 0.0f, 0.0f );
        glTranslatef(0.0f, (GLfloat)bitmap_glyph->top-bitmap.rows, 0.0f);
        float   x=(float)bitmap.width / (float)width,
            y=(float)bitmap.rows / (float)height;
        glBegin(GL_QUADS);
        glTexCoord2d(0,0); glVertex3f( 0, 0, 1 );                       //Bottom-left vertex (corner) 
        glTexCoord2d(0,y); glVertex3f( 0, (GLfloat)bitmap.rows, 1 );    // top left vertex
        glTexCoord2d(x,y); glVertex3f( (GLfloat)bitmap.width, (GLfloat)bitmap.rows, 1 ); // top right vertex
        glTexCoord2d(x,0); glVertex3f( (GLfloat)bitmap.width, 0, 1 );   // bottom right
        glEnd();
        glPopMatrix();
        glTranslatef( (GLfloat)( ttf.ftFace_->glyph->advance.x >> 6 ), 0, 0);
        glEndList();
                I might be completely off, but this looks suspicious: glNewList(fontData_.fontBase_+i, GL_COMPILE); The first parameter should be a unique number you get from glGenLists(), but from this code it is not obvious if you're doing that.
– jrok
                Dec 4, 2011 at 19:52
                It is kind of impossible to determine the reason of the leak. Read the docu very good. Probably some functions you are using allocates something that you aren't freeing.
– FailedDev
                Dec 4, 2011 at 20:05
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.