Skip to main content
GameDev.net gamedev.net
🔒 Locked

SDL : Is this safe?

Started by Super Robot Fox Sep 10, 2004 at 5:40 PM 7 replies 2.3k views
Original Post
Super Robot Fox
Super Robot Fox
I think here I am creating a surface on the stack by creating a surface (on the heap?) and dereferencing the pointer, thus copying it (the data at the pointer).

SDL_Surface someSurface = *SDL_CreateRGBSurface(...)
The problem is, how do I free the memory I created, as I can't use SDL_FreeSurface() on someSurface? Forgive me if I've got this all wrong!
Grellin GDU
Grellin GDU
Example    /* Create a 32-bit surface with the bytes of each pixel in R,G,B,A order,       as expected by OpenGL for textures */    SDL_Surface *surface;    Uint32 rmask, gmask, bmask, amask;    /* SDL interprets each pixel as a 32-bit number, so our masks must depend       on the endianness (byte order) of the machine */#if SDL_BYTEORDER == SDL_BIG_ENDIAN    rmask = 0xff000000;    gmask = 0x00ff0000;    bmask = 0x0000ff00;    amask = 0x000000ff;#else    rmask = 0x000000ff;    gmask = 0x0000ff00;    bmask = 0x00ff0000;    amask = 0xff000000;#endif    surface = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32,                                   rmask, gmask, bmask, amask);    if(surface == NULL) {        fprintf(stderr, "CreateRGBSurface failed: %s\n", SDL_GetError());        exit(1);    }


"If you are not willing to try, you will never succeed!"GrellinC++ Game Programming
Sneftel
Sneftel
My gut feeling: this seems like a really bad idea. Why would you want to do this, anyway?
Super Robot Fox
Super Robot Fox
I was having some trouble copying a surface pointer to an object. The pointer was passed to the objects constructor, but then the pointer went out of scope. I thought, since the memory and the pointer are independant (I thought wrong maybe?), that the memory would still be allocated at the location pointed to by the pointer passed to the object, so it would still be dereferencable after the original pointer had gone out of scope. Was I right in thinking this? Anyway, long story short, I found if I used the method in my first post, the error disappeared.

Sorry if that was a poor explenation, Im knackered, I'll go through my code tomorrow and remove that "fix" I did. Thanks a lot for the help guys.
Monder
Monder
You thought right. When you have a pointer to something in memory it stays allocated till you explicitly free it (unless you've got some kind of smart pointer and reference couting scheme, but you'd be aware of it if you did). What you're doing here will cause a memory leak as SDL_CreateRGBSurface is allocated new memory for the surface, you make a shallow copy of the surface it produces and the memory it has allocated is left unfreed, you do not want to do this.
Leffe
Leffe
Quote:
Original post by Monder
You thought right. When you have a pointer to something in memory it stays allocated till you explicitly free it (unless you've got some kind of smart pointer and reference couting scheme, but you'd be aware of it if you did). What you're doing here will cause a memory leak as SDL_CreateRGBSurface is allocated new memory for the surface, you make a shallow copy of the surface it produces and the memory it has allocated is left unfreed, you do not want to do this.


Actually, I think SDL uses some kind of reference counting... I'll just have to look it up...

Ah, here, I don't know anything more about that apart from that one comment though.
Super Robot Fox
Super Robot Fox
Ah, didn't realise this thread was still being replied to. I think found my problem, turns out I was calling SDL_FreeSurface in the object's destructor, which was being called when the original object went out of scope (after a copy was being placed in a STL vector). The surface being referred to (by the object in the vector) was indeed freed by the destructor; because I overlooked a copy constructor for the object, the pointer had just been memberwise copied... so pointed to the previously freed memory. Oops! Segmentation fault!

Moral of the story: read up on copy constructors!
swiftcoder
swiftcoder
Quote:
Original post by Leffe
Actually, I think SDL uses some kind of reference counting... I'll just have to look it up...
Ah, here, I don't know anything more about that apart from that one comment though.

There is a reference_count data member in SDL_Surface, but I have never figured out why it is there, SDL never seems to modify its value, and SDL has no garbage collection as far as I know, so it seems redundant (maybe a left-over for previous version compatibility?).
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
Roots
Roots
Quote:
Original post by swiftcoder
Quote:
Original post by Leffe
Actually, I think SDL uses some kind of reference counting... I'll just have to look it up...
Ah, here, I don't know anything more about that apart from that one comment though.

There is a reference_count data member in SDL_Surface, but I have never figured out why it is there, SDL never seems to modify its value, and SDL has no garbage collection as far as I know, so it seems redundant (maybe a left-over for previous version compatibility?).


My guess is that the designers of SDL put it in there for users so that if they wanted to they could use the reference count member. I don't think they ever had the intention of SDL using the reference count directly, perhaps because the "S" in SDL stands for simple. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.