Thread: SDL: Garbage collection (?) screwing me

  1. #1
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212

    SDL: Garbage collection (?) screwing me

    I've made a function to load a bitmap and give me a pointer to it.
    Code:
    void loadBMP(char *fname, SDL_Surface *bmp)
    {
        SDL_Surface *tmp = SDL_LoadBMP(fname);
        
        char msg[256];
        
        if (tmp == NULL) {
    	   sprintf (msg, "Couldn't initialize: %s\n", SDL_GetError ());
           MessageBox (0, msg, "Error", MB_ICONHAND); 
    	   return;
        }
    
        bmp = SDL_DisplayFormat(tmp);
        SDL_FreeSurface(tmp);
    }
    But it only seems to stay allocated within the function for some reason.

    e.g. if I do
    Code:
    sprintf(msg, "%d", bmp->w);
    MessageBox (0, msg, "Error", MB_ICONHAND);
    from within the function, it gives me the width of the bitmap, however if I call that outside the function

    Code:
    SDL_Surface *bitmap;
    loadBitmap("block.bmp", bitmap);
    sprintf(msg, "%d", bitmap->w);
    MessageBox (0, msg, "Error", MB_ICONHAND);
    It crashes the program. How can I stop the data from being garbage collected?
    Last edited by Brian; 05-07-2005 at 06:26 PM.

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Code:
    void loadBMP(char *fname, SDL_Surface *bmp)
    I notice here you're passing the address of an SDL_Surface.
    A local variable within the function called bmp is used to store that address.

    Code:
    bmp = SDL_DisplayFormat(tmp);
    Here you assign the value returned by SDL_DisplayFormat to bmp. But remember, bmp is a local pointer. It does not affect the pointer outside the function.

    Code:
    SDL_Surface *bitmap;
    loadBitmap("block.bmp", bitmap);
    Here, bitmap's value is not changed at all.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    Quote Originally Posted by Dante Shamest
    Code:
    void loadBMP(char *fname, SDL_Surface *bmp)
    I notice here you're passing the address of an SDL_Surface.
    This creates a local variable within the function called bmp.

    Code:
    bmp = SDL_DisplayFormat(tmp);
    Here you assign the value returned by SDL_DisplayFormat to bmp. But remember, bmp is a local pointer. It does not affect the pointer outside the function.

    Code:
    SDL_Surface *bitmap;
    loadBitmap("block.bmp", bitmap);
    Here, bitmap's value is not changed at all.
    Haha, you're right. Thanks for the quick response. I'm a retard.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    To save passing useless uninitialised values to functions which you're just going to overwrite anyway...
    Code:
    SDL_Surface * loadBMP(char *fname)
    {
        SDL_Surface *bmp;
        SDL_Surface *tmp = SDL_LoadBMP(fname);
        
        char msg[256];
        
        if (tmp == NULL) {
    	   sprintf (msg, "Couldn't initialize: %s\n", SDL_GetError ());
               MessageBox (0, msg, "Error", MB_ICONHAND); 
    	   return NULL;
        }
    
        bmp = SDL_DisplayFormat(tmp);
        SDL_FreeSurface(tmp);
        return bmp;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    Write your code like Salem's but remember: you ought to test the return of SDL_DisplayFormat and return the bitmap in system memory if you do not have enough video memory.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. garbage collection enabled c++
    By manav in forum C++ Programming
    Replies: 42
    Last Post: 02-15-2008, 01:42 PM
  2. Garbage Collection is not so complicated
    By Nalpdii in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 11:34 PM
  3. C++ Garbage Collection
    By vaibhav in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2005, 10:26 AM
  4. Garbage Collection
    By Orborde in forum C++ Programming
    Replies: 4
    Last Post: 05-10-2005, 11:18 PM
  5. garbage collection
    By joed in forum C Programming
    Replies: 4
    Last Post: 04-01-2004, 01:47 PM