[SDL] [SDL/C++] Garbage Collection

Peter Mackay mackay.pete+sdl at gmail.com
Tue Jan 27 07:05:16 PST 2009


For those looking to shift handling of SDL surfaces to a RAII object,
Boost's intrusive_ptr works pretty well for this. Something like:

typedef boost::intrusive_ptr<SDL_Surface> SurfacePtr;

Then you need to provide the following connections into SDL's surface
reference counting setup:

void intrusive_ptr_add_ref(SDL_Surface* surface)
{
    ++surface->refcount; // Not sure "refcount" is the exact name.
}

void intrusive_ptr_release(SDL_Surface* surface)
{
    SDL_FreeSurface(surface);
}

Just make sure that when you initially pass your raw pointer to
SurfacePtr's constructor that you pass the correct value to the
"addref" parameter (it defaults to true). Otherwise it may add a
reference, which because newly created surfaces already have refcount
== 1, you may not want.

Peter

2009/1/27 Jonathan Dearborn <grimfang4 at gmail.com>:
> Most of the time, my projects are small enough to make manual memory
> management easy or I use some form of the RAII method mentioned.  I have
> made a class specifically for juggling SDL_Surfaces, though.  To me, it
> depends on the project.  For a game, I would not use garbage collection, but
> try to be smart about memory allocation and deletion (so that it rarely
> happens in-game).
>
> Jonny D
>
>
> On Tue, Jan 27, 2009 at 8:53 AM, Christoph Friedrich
> <seija at dragons-server.de> wrote:
>>
>> Thanks for the quick answers.
>> What do you use?
>> Clean up memory manually or do you use some type of garbage collection?
>> (This question goes to the complete mailing list ^^)
>>
>> Greets
>> Christoph
>>
>> julien CLEMENT schrieb:
>>
>> Check out this:
>> http://www.hpl.hp.com/personal/Hans_Boehm/gc/
>>
>> Take time to read the limitations
>>
>> Julien
>>
>> --- En date de : Mar 27.1.09, Brian <brian.ripoff at gmail.com> a écrit :
>>
>> De: Brian <brian.ripoff at gmail.com>
>> Objet: Re: [SDL] [SDL/C++] Garbage Collection
>> À: "A list for developers using the SDL library. (includes SDL-announce)"
>> <sdl at lists.libsdl.org>
>> Date: Mardi 27 Janvier 2009, 14h04
>>
>> Hello.
>>
>> A fairly clean method of handling any resource (memory isn't the only
>> resource Java!) is RAII
>> (http://en.wikipedia.org/wiki/Resource_acquisition_is_initialization).
>> Using the standard C++ classes (std::string, std::vector<> and other
>> containers) instead of trying
>>  to juggle pointers. Using boost's smart
>> pointer library elsewhere
>> (http://www.boost.org/doc/libs/1_37_0/libs/smart_ptr/smart_ptr.htm).
>>
>> If looking at all those templates makes your eyes bleed, liberal use
>> of "typedef" can really help.
>>
>> When all else fails hand writing your own RAII wrapper classes.
>>
>> Just be aware of the limitations, in particular circular references
>> when using reference counted smart pointers.
>>
>> Hope this gives you some ideas,
>>       Brian.
>>
>> On Tue, Jan 27, 2009 at 12:59 PM, Christoph Friedrich
>> <seija at dragons-server.de> wrote:
>> > Hi,
>> >
>> > does anybody know a good garbage collection library I can use in my game
>> > which is portable like SDL?
>> >
>> > How do you manage dynamic memory?
>> _______________________________________________
>> SDL mailing
>>  list
>> SDL at lists.libsdl.org
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>>
>>
>> ________________________________
>> _______________________________________________
>> SDL mailing list
>> SDL at lists.libsdl.org
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>>
>>
>> _______________________________________________
>> SDL mailing list
>> SDL at lists.libsdl.org
>> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>>
>
>
> _______________________________________________
> SDL mailing list
> SDL at lists.libsdl.org
> http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
>
>



More information about the SDL mailing list