Bug with threads in SDL 1.2.7 on MacOSX

Hi.

I tried to run my app on Mac OSX (10.3). (I develop usually on linux)
My app crashed with a “BusError”.
I got it narrowed down to the lines:

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);
SDL_SetVideoMode(320, 240, 32, | SDL_SWSURFACE);

After theese lines the app will crash after less than a second.
(it actually executes code even after the SetVideoMode-call, but it will
crash. Try with eg. while(1); )

If I remove the SDL_INIT_EVENTTHREAD, then SetVideoMode works, but my threads
don’t…

SDL_Init doesn’t return a value <0, nor does SDL_SetVideoMode return a NULL
pointer.
And the code works on Linux (with SDL 1.2.5).

SDL 1.2.7 was installed with a “./configure --prefix=/some/where/ && make &&
make install” -line

br
Kalle Raiskila

“Kalle” wrote in message
news:loom.20040719T084138-72 at post.gmane.org

Hi.

I tried to run my app on Mac OSX (10.3). (I develop usually on linux)
My app crashed with a “BusError”.
I got it narrowed down to the lines:

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);
SDL_SetVideoMode(320, 240, 32, | SDL_SWSURFACE);

I don’t know if it’s an error but on the SetVideoMode, you’ve got “|
SDL_SWSURFACE”… is the | necessary?

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);
SDL_SetVideoMode(320, 240, 32, | SDL_SWSURFACE);

This example code isn’t even valid C, it looks like perhaps you forgot
to write something there. Why not put your source code somewhere we can
see it and see if there are any obvious problems.

Nick Howes <n.j.howes warwick.ac.uk> writes:

I don’t know if it’s an error but on the SetVideoMode, you’ve got “|
SDL_SWSURFACE”… is the | necessary?

Nope, its a copy-paste-typo :slight_smile:

But anyhow, I didn’t find any difference in what flags I pass to
SetVideoMode. It’s just the SDL_INIT_EVENTTHREAD that causes trouble.

Donny Viszneki <smirk thebuicksix.com> writes:

SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD);
SDL_SetVideoMode(320, 240, 32, | SDL_SWSURFACE);

This example code isn’t even valid C, it looks like perhaps you forgot
to write something there. Why not put your source code somewhere we can
see it and see if there are any obvious problems.

Sorry, c&p this into bug.c
------------8<------------------------------------------------
#include <SDL.h>

int main(int argc, char* argv[]){
SDL_Surface *surface;

if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD ) < 0) exit(1);
surface = SDL_SetVideoMode( 320, 240, 32, SDL_SWSURFACE);

// wait for chrash:
while(1);

return 0;
}
------------8<------------------------------------------------
HTH
Kalle

#include <SDL.h>

int main(int argc, char* argv[]){
SDL_Surface *surface;

if( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD ) < 0) exit(1);
surface = SDL_SetVideoMode( 320, 240, 32, SDL_SWSURFACE);

// wait for chrash:
while(1);

return 0;
}

I made one minor change, exit became return. Results are below. Anyone
have any ideas?

$ c++ -g2 -ggdb -gstabs+ sdl-config --cflags --libs test.cpp

Program received signal EXC_BAD_ACCESS, Could not access memory.
[Switching to process 1225 thread 0x3c07]
0x0023e8bc in QZ_PumpEvents (this=0x1812400) at
/System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h:59
59 FOUNDATION_STATIC_INLINE NSRect NSMakeRect(float x, float y,
float w, float h) {
(gdb) bt
#0 0x0023e8bc in QZ_PumpEvents (this=0x1812400) at
/System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h:59
#1 0x0024385c in SDL_GobbleEvents (unused=0x110baa0) at
SDL_events.c:102
#2 0x0024385c in SDL_GobbleEvents (unused=0x110baa0) at
SDL_events.c:102
#3 0x0024c984 in SDL_RunThread (data=0x113fbb0) at SDL_thread.c:218
#4 0x0024cb88 in RunThread (data=0x110baa0) at SDL_systhread.c:82
#5 0x900246e8 in _pthread_body ()

I just tried to run src/tests/threadwin.c, here is the synopsis of what
happened (it runs fine when I don’t use the -threaded option):

~/darwinports/dports/devel/libsdl/work/SDL-1.2.7/test$ cc -g2 -ggdb
-gstabs+ sdl-config --cflags --libs threadwin.c
~/darwinports/dports/devel/libsdl/work/SDL-1.2.7/test$ mv a.out
threadwin
~/darwinports/dports/devel/libsdl/work/SDL-1.2.7/test$ gdb threadwin

. . .

(gdb) set args -threaded
(gdb) run
Starting program:
/Users/donny/darwinports/dports/devel/libsdl/work/SDL-1.2.7/test/
threadwin -threaded
Running with threaded events
Transparent pixel: (255,255,255)

Program received signal EXC_BAD_ACCESS, Could not access memory.
[Switching to process 1300 thread 0x3c07]
0x002378bc in QZ_PumpEvents (this=0x1812400) at
/System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h:59
59 FOUNDATION_STATIC_INLINE NSRect NSMakeRect(float x, float y,
float w, float h) {
(gdb) bt
#0 0x002378bc in QZ_PumpEvents (this=0x1812400) at
/System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h:59
#1 0x0023c85c in SDL_GobbleEvents (unused=0x110bb40) at
SDL_events.c:102
#2 0x0023c85c in SDL_GobbleEvents (unused=0x110bb40) at
SDL_events.c:102
#3 0x00245984 in SDL_RunThread (data=0x11402f0) at SDL_thread.c:218
#4 0x00245b88 in RunThread (data=0x110bb40) at SDL_systhread.c:82
#5 0x900246e8 in _pthread_body ()
(gdb) thread 1
[Switching to thread 1 (process 1300 local thread 0x1107)]
#0 0x900074c8 in mach_msg_trap ()
(gdb) bt
#0 0x900074c8 in mach_msg_trap ()
#1 0x90007018 in mach_msg ()
#2 0x936097bc in _CGSSynchronizeWindowBackingStore ()
#3 0x935dcd84 in _CGSSynchronizeWindow ()
#4 0x935d8804 in _CGSLockWindow ()
#5 0x935eff94 in CGSLockWindowRectBits ()
#6 0x9156de08 in QDPlatformLockPortBits ()
#7 0x9156f160 in LockPortBits ()
#8 0x00239ab8 in QZ_SetVideoWindowed (this=0x1154ce0,
current=0x11356d0, width=40, height=44, bpp=32, flags=0) at
SDL_QuartzVideo.m:795
#9 0x00239c58 in QZ_SetVideoMode (this=0x1154ce0, current=0xa1563a4c,
width=40, height=44, bpp=-8984, flags=0) at SDL_QuartzVideo.m:842
#10 0x002321e8 in SDL_SetVideoMode (width=640, height=480, bpp=8,
flags=18044624) at SDL_video.c:647
#11 0x00003f70 in SDL_main (argc=Cannot access memory at address
0xffffe190
) at threadwin.c:276
#12 0x000030d0 in -[SDLMain applicationDidFinishLaunching:] (self=0x0,
_cmd=0x3, note=0x28) at macosx/SDLMain.m:223
#13 0x909f7184 in _nsnote_callback ()
#14 0x901aa650 in __CFXNotificationPost ()
#15 0x901af060 in _CFXNotificationPostNotification ()
#16 0x909f4fd0 in -[NSNotificationCenter
postNotificationName:object:userInfo:] ()
#17 0x92ee37b8 in -[NSApplication _postDidFinishNotification] ()
#18 0x92ee36a4 in -[NSApplication _sendFinishLaunchingNotification] ()
#19 0x92ee38d4 in -[NSApplication(NSAppleEventHandling)
_handleCoreEvent:withReplyEvent:] ()
#20 0x90a4a544 in -[NSAppleEventManager
dispatchRawAppleEvent:withRawReply:handlerRefCon:] ()
#21 0x90a50c6c in _NSAppleEventManagerGenericHandler ()
#22 0x91674a68 in aeDispatchAppleEvent(AEDesc const*, AEDesc*, unsigned
long, unsigned char*) ()
#23 0x916778b0 in dispatchEventAndSendReply(AEDesc const*, AEDesc*) ()
#24 0x91674ca8 in aeProcessAppleEvent ()
#25 0x928c47f4 in AEProcessAppleEvent ()
#26 0x92dd2c0c in _DPSNextEvent ()
#27 0x92de93b0 in -[NSApplication
nextEventMatchingMask:untilDate:inMode:dequeue:] ()
#28 0x92dfd718 in -[NSApplication run] ()
#29 0x00003058 in CustomApplicationMain (argc=0, argv=3) at
macosx/SDLMain.m:201
#30 0x0000331c in main (argc=17994976, argv=0xa2dc0078) at
macosx/SDLMain.m:302

The source code does seem to say that it isn’t supported on all
operating systems:

./include/SDL.h:#define SDL_INIT_EVENTTHREAD 0x01000000 /* Not
supported on all OS’s */

Clearly part of the problem is there doesn’t seem to be much
documentation for it. Or at least part of /my/ problem is i don’t have
any documentation for it.

I made one minor change, exit became return. Results are below. Anyone
have any ideas?

$ c++ -g2 -ggdb -gstabs+ sdl-config --cflags --libs test.cpp

Program received signal EXC_BAD_ACCESS, Could not access memory.
[Switching to process 1225 thread 0x3c07]
0x0023e8bc in QZ_PumpEvents (this=0x1812400) at
/System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h:59
59 FOUNDATION_STATIC_INLINE NSRect NSMakeRect(float x, float y,
float w, float h) {
(gdb) bt
#0 0x0023e8bc in QZ_PumpEvents (this=0x1812400) at

You should probably pump the events from the event queue in the main loop of your program.
It seems like the event queue is overflowing.

Stephane

Hey Kalle, I don’t really know much about SDL_INIT_EVENTTHREAD, there
doesn’t seem to be much information about it. But from reading the SDL
source, it seems to use a separate thread to manage the event queue. I
don’t think this is needed for multi-threaded applications. Why don’t
you try running your application without it?

Donny Viszneki <smirk thebuicksix.com> writes:

The source code does seem to say that it isn’t supported on all
operating systems:

./include/SDL.h:#define SDL_INIT_EVENTTHREAD 0x01000000 /* Not
supported on all OS’s */

According to the documentation this means MacOS Classic.
If this is the case on MacOSX, too, then having SDL_Init returning
a -1 would be nice.

I made one minor change, exit became return. Results are below. Anyone
have any ideas?

$ c++ -g2 -ggdb -gstabs+ sdl-config --cflags --libs test.cpp

Program received signal EXC_BAD_ACCESS, Could not access memory.
[Switching to process 1225 thread 0x3c07]
0x0023e8bc in QZ_PumpEvents (this=0x1812400) at
/System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h:
59
59 FOUNDATION_STATIC_INLINE NSRect NSMakeRect(float x, float y,
float w, float h) {
(gdb) bt
#0 0x0023e8bc in QZ_PumpEvents (this=0x1812400) at

You should probably pump the events from the event queue in the main
loop of your program.
It seems like the event queue is overflowing.

That’s a possibility. I kind of took it for granted from Kalle that
THAT wasn’t the problem. I’ll check it out.

On a side note, I personally think that’s a terrible problem if the
result of letting SDL’s event queue to fill up is a seg fault / bus
error.On Jul 19, 2004, at 7:18 AM, Stephane Marchesin wrote:

Stephane


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

This is not a repeat, I just wanted to continue on to say that I tried
another test program (threadwin.c that comes with SDL.) Read my other
posts in the thread, though you probably already have by now :)On Jul 19, 2004, at 7:18 AM, Stephane Marchesin wrote:

I made one minor change, exit became return. Results are below. Anyone
have any ideas?

$ c++ -g2 -ggdb -gstabs+ sdl-config --cflags --libs test.cpp

Program received signal EXC_BAD_ACCESS, Could not access memory.
[Switching to process 1225 thread 0x3c07]
0x0023e8bc in QZ_PumpEvents (this=0x1812400) at
/System/Library/Frameworks/Foundation.framework/Headers/NSGeometry.h:
59
59 FOUNDATION_STATIC_INLINE NSRect NSMakeRect(float x, float y,
float w, float h) {
(gdb) bt
#0 0x0023e8bc in QZ_PumpEvents (this=0x1812400) at

You should probably pump the events from the event queue in the main
loop of your program.
It seems like the event queue is overflowing.

Stephane


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

Donny Viszneki <smirk thebuicksix.com> writes:

Hey Kalle, I don’t really know much about SDL_INIT_EVENTTHREAD, there
doesn’t seem to be much information about it. But from reading the SDL
source, it seems to use a separate thread to manage the event queue. I
don’t think this is needed for multi-threaded applications. Why don’t
you try running your application without it?

Hmm. I did try to run my app without SDL_INIT_EVENTTHREAD.
I had one thread for parsing the SDL_Events running a loop like:

while(SDL_WaitEvent(&event)){
//code
}

(I can post the entire code, but it is so awfull that I hope the
idea is enough :wink:

On MacOSX this thread didn’t recieve any events.

On Linux I haven’t yet tested it. (will test as soon as I get a chance)
(And with SDL_INIT_EVENTTHREAD, the loop worked fine)

This might be just a bug in my app, though I don’t think so… :wink:

Kalle

Donny Viszneki <smirk thebuicksix.com> writes:

You should probably pump the events from the event queue in the main
loop of your program.
It seems like the event queue is overflowing.

That’s a possibility. I kind of took it for granted from Kalle that
THAT wasn’t the problem. I’ll check it out.

The crash did come before my app started looking for SDL-events.
But, OTOH, I didn’t notice anythig untill I switched to Mac…> On Jul 19, 2004, at 7:18 AM, Stephane Marchesin wrote:

This might be just a bug in my app, though I don’t think so… :wink:
Kalle

I experienced the same problem 6 months ago, and gave up with trying
to use threaded events in my OSX code… haven’t noticed a problem
since then, but actually as it turns out it would actually be nice to
move the SDL event handler to a separate thread, so if you end up
with a solution to this, I’ll be interested in it … alas, no time
to look further into the problem with you, though …–

;

Jay Vaughan

On Linux I haven’t yet tested it. (will test as soon as I get a chance)
(And with SDL_INIT_EVENTTHREAD, the loop worked fine)

But, OTOH, I didn’t notice anythig untill I switched to Mac…

Sometimes you seem to be saying that your code WORKS on Linux, and in
some places you seem to imply that you haven’t tried it on Linux.
Perhaps this is just me being stupid, but which is it?

Donny Viszneki <smirk thebuicksix.com> writes:

Sometimes you seem to be saying that your code WORKS on Linux, and in
some places you seem to imply that you haven’t tried it on Linux.
Perhaps this is just me being stupid, but which is it?

Sorry,

My app, inited with EVENTTHREAD, works on Linux.
My app, inited with EVENTTHREAD, crashes on Mac.

My app, inited without EVENTTHREAD, doesn’t work on Mac (look previous posts).
My app, inited without EVENTTHREAD, I haven’t tested on Linux.

I read SDL-sources yesterday, and found that EVENTTHREAD is on the TODO/wishlist
for win32 and Mac. So that solves that…

But. Does threads work on MacOSX? runnig the testthread program that came with
SDL-1.2.7 suggests that they don’t. Are they supposed to?
(the testthread program spits out something like:
Killing thread 2
Thread 2 is alive
Thread 2 is alive
Thread 2 is alive

)
Or is it just me that cannot code :slight_smile:

I read SDL-sources yesterday, and found that EVENTTHREAD is on the
TODO/wishlist
for win32 and Mac. So that solves that…

But. Does threads work on MacOSX? runnig the testthread program that
came with
SDL-1.2.7 suggests that they don’t. Are they supposed to?
(the testthread program spits out something like:
Killing thread 2
Thread 2 is alive
Thread 2 is alive
Thread 2 is alive

Earlier I posted about running “threadwin.c” in the test directory of
SDL and it didn’t work. I don’t know why it wouldn’t if it works on
linux (unless the linux code isn’t just generic unix threading, which
in my opinion, it probably should be.)

Oh well, looks like this thread is over. BTW, could you email me
directly and tell me what exactly the functionality of
SDL_INIT_EVENTTHREAD is supposed to be?

Donny Viszneki <smirk thebuicksix.com> writes:

Sometimes you seem to be saying that your code WORKS on Linux, and in
some places you seem to imply that you haven’t tried it on Linux.
Perhaps this is just me being stupid, but which is it?

Sorry,

My app, inited with EVENTTHREAD, works on Linux.
My app, inited with EVENTTHREAD, crashes on Mac.

My app, inited without EVENTTHREAD, doesn’t work on Mac (look previous
posts).
My app, inited without EVENTTHREAD, I haven’t tested on Linux.

I read SDL-sources yesterday, and found that EVENTTHREAD is on the
TODO/wishlist
for win32 and Mac. So that solves that…

That refers to classic MacOS.

I never though about EVENTHREAD on OS X, to be frank I wasn’t even
aware it existed :-). But I am not at all surprised that it doesn’t
work out of the box. To quote the docs:

“To use Cocoa from multiple threads, the Cocoa framework must be put
into multithreading mode, which activates greater thread safety
measures. This happens automatically when the first NSThread is
detached. If you use the POSIX thread APIs to create threads, your
application must detach at least one NSThread, which may immediately
exit, before any pthreads can use Cocoa.”

Even with that, i am not sure it’ll be enough. But I’ll try and see if
I can come up with a patch to fix the problem.

But. Does threads work on MacOSX? runnig the testthread program that
came with
SDL-1.2.7 suggests that they don’t. Are they supposed to?
(the testthread program spits out something like:
Killing thread 2
Thread 2 is alive
Thread 2 is alive
Thread 2 is alive

That’s a known issue with pthread_cancle / pthread_kill on Mac OS X
(the code didn’t work in 10.0; it did work in, I think 10.2; and it’s
not working anymore in 10.3; or something like that ;-). So most of the
thread code should work fine, just killing threads won’t. Not that
terribly hard to work around in most cases, though, just let your
thread finish by itself (i.e. if you thread function basically is an
endless loop, just change it to
while (!thisThreadShouldFinish) {

}
If you want to stop the thread, set “thisThreadShouldFinish” to true in
your main thread.

)
Or is it just me that cannot code :slight_smile:

Bye,

MaxAm 20.07.2004 um 09:08 schrieb Kalle: