Kann memory leak in Ladefunktion nicht finden :(



  • Guten Tag.

    Ich habe eine Funktion zum laden von Texturen, welche mit VS und Windows problemlos funktioniert leicht abgeändert, damit sie unter Linux mit g++ compiled - z.B. Byte definiert.

    Das funktionierte soweit auch (Textur wird korrekt geladen und angezeigt). Allerdings werden die 8GB Ram recht schnell voll und dann gehts abwärts...

    Hier die Funktion:

    #include "GL/gl.h"
    #include "GL/glu.h"
    #include "stdio.h"
    #include "stdlib.h"
    
    namespace KGLL {
    
      typedef unsigned char BYTE;   // 8-bit unsigned entity.
    
    GLuint LoadTextureRAW( const char * filename, int wrap )
    {
      GLuint texture;
      int width, height;
      BYTE * data;
      FILE *file;
    
      // open texture data
      file = fopen( filename, "rb" );
      if ( file == NULL ) return 0;
      // allocate buffer
      width = 1024;
      height = 1024;
    
      data = (BYTE *) malloc( width * height * 3 );
      // read texture data
      fread( data, width * height * 3, 1, file );
      fclose( file );
      // allocate a texture name
      glGenTextures( 1, &texture );
      // select our current texture
      glBindTexture( GL_TEXTURE_2D, texture );
      // select modulate to mix texture with color for shading
      glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
      // when texture area is small, bilinear filter the closest MIP map
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
      // when texture area is large, bilinear filter the first MIP map
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
      // if wrap is true, the texture wraps over at the edges (repeat)
      //       ... false, the texture ends at the edges (clamp)
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP );
      glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP );
      // build our texture MIP maps
       gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data );
      // free buffer
      free( data );
      return texture;
    }
    

    Jemand eine Idee?

    Unter Windows war noch anders:

    data = malloc( width * height * 3 );
    

    Ich programmiere normalerweise mit Delphi und habe daher nur begrenztes C++ Wissen.

    Vielen Dank.



  • Hallo kgh,

    vermutlich gibst du deine Texturen nicht wieder frei, nachdem sie nicht mehr benutzt werden.

    void glDeleteTextures(GLsizei n, const GLuint *textures);
    

    Viele Grüße,
    MaBa



  • Hey super! Genau daran lag es!

    Vielen Dank!



  • Kleiner Tip: schau dir mal C++ an. Außer dem Namespace war das alles C:

    - "stdio.h" und "stdlib.h" heißen in C++ <cstdio> und <cstdlib>
    - Für Dateioperationen gibts fstreams, die schließen sich beispielsweise von selber (Stichwort RAII)
    - statt malloc benuttz man in C++ meist new - und statt new für einfache byte-Arrays kann man auch vector benutzen, der den Speicher automatisch wieder freigibt.


Anmelden zum Antworten