Thread: Memory leaks

  1. #1
    Registered User
    Join Date
    Aug 2001

    Memory leaks

    What are some of the common problems that cause memory leaks?

    would the char* in this cause one?

    unsigned int loadtex(char*name) {
    unsigned int img,texture;
    ilGenImages(1, &img); ilBindImage(img); ilLoad(IL_JPG,name); texture = ilutGLBindMipmaps();
    ilDeleteImages(1, &img);
    return texture;

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    difficult to say without knowing how the memory that the char* points to was allocated.

    Memory leaks occur when you use malloc() and forget to call free(), or similarly when you call new and forget to call delete.

    for instance....

    char* name="";
    // your code here
    // no memory leaks

    name was statically allocated and memory taken care of by your compiler.

    char* name=new char[ 13 ];
    strcpy (name,"");
    // call your func
    // if you then do this.....
    delete [] name;
    // there is no memory leak

    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :-

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Waterloo, Texas

    Consider this:

    char *DoSomething(const char *in)
    char *out = malloc(strlen(in));
    return out;
    int main()
    char *in = malloc(100);  // allocation...
    char withThisString[] = "A day in the life of a programmer";
    int x = 100;
    while(x-- > 0)
    in = DoSomething(withThisString);
    //, 'out' gets malloc'd every loop!...
    <<  Memory Leak, 10 K of memory... <<
    free(in);  //<--Which is free'd here? 
    return 0;
    <<  Memory Leak ??  <<

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Another "popular", (i.e. common), cause of, (diificult to find), memory leaks is using non thread safe stdlib calls in a multithreaded environment.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for memory leaks
    By Bladactania in forum C Programming
    Replies: 5
    Last Post: 02-10-2009, 12:58 PM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Tons of memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2005, 10:19 AM
  4. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM
  5. about memory leaks with this simple program
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 07:19 PM