[SDL] [patch] OS X carbon SDL_WINDOWID support

earl earl at stanfordalumni.org
Sat Sep 23 23:36:36 PDT 2006


Hi Bob,

On 9/22/06,Bob Ippolito <bob at redivi.com> wrote:

> That's really not how you want to do it, unless the threads are short
> lived. The idiom for maintaining NSAutoreleasePool is that you should
> release them periodically, the main thread does this after each
> iteration of the event loop for example. Otherwise you're effectively
> leaking memory for the lifetime of the thread.

Thanks for setting me straight on this.  I am not a cocoa developer.

> What you're seeing means one of two things: you're either doing
> something that you shouldn't, or SDL isn't guaranteeing an
> NSAutoreleasePool in the safe-for-alternate-thread functions where it
> happens to use Objective-C.

The SDL FAQ reminds us that "most graphics back-ends are not
thread-safe".  However it should be OK to call SDL video functions
strictly from a single thread even if that thread is not the main thread
of an application, right?

Other than leaking these cocoa objects, calling SDL video functions from
a single thread is working flawlessly for me on Windows, Linux/X11, and
OS X.  This lack of an autorelease pool for the Quartz video code is the
only SDL bug I'm having to work around.  Perhaps this could be fixed in
SDL, even though it doesn't change the fact that you shouldn't call SDL
video functions from multiple threads.

> Only the latter should be patched, but
> someone would have to track down exactly where this is happening (the
> stderr spew should say which function to set a breakpoint on).

For the record, here's a trivial test program followed by the errors
(without the timestamps printed on each line).  Unfortunately the errors
don't reveal source code line numbers.

#include <SDL.h>
int test_thread(void*data)
{
        SDL_SetVideoMode(100, 200, 0,
               
SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_HWACCEL|SDL_NOFRAME|SDL_RESIZABLE);
        return 0;
}
int main(int argc, char* argv[]) {
        SDL_Init (SDL_INIT_VIDEO);
        SDL_CreateThread(test_thread, NULL);
        SDL_Delay(1000); /* crudely wait for the thread to finish */
}

*** _NSAutoreleaseNoPool(): Object 0x11137a0 of class NSCFNumber
autoreleased with no pool in place - just leaking
*** _NSAutoreleaseNoPool(): Object 0x112c360 of class NSView
autoreleased with no pool in place - just leaking
*** _NSAutoreleaseNoPool(): Object 0x112cd60 of class
SDL_QuartzWindowDelegate autoreleased with no pool in place - just leaking
*** _NSAutoreleaseNoPool(): Object 0x112e310 of class NSIdEnumerator
autoreleased with no pool in place - just leaking
*** _NSAutoreleaseNoPool(): Object 0xa3712254 of class NSCFString
autoreleased with no pool in place - just leaking
*** _NSAutoreleaseNoPool(): Object 0x1111440 of class NSCFArray
autoreleased with no pool in place - just leaking
*** _NSAutoreleaseNoPool(): Object 0x1133670 of class NSView
autoreleased with no pool in place - just leaking
*** _NSAutoreleaseNoPool(): Object 0x1133670 of class NSView
autoreleased with no pool in place - just leaking

A non-trivial application using other SDL video functions in the thread
will produce errors about other leaked objects as well.

thanks
earl





More information about the SDL mailing list