We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 1712 - Subsystem refcounting is trashed by SDL_Init
Summary: Subsystem refcounting is trashed by SDL_Init
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: main (show other bugs)
Version: HG 2.0
Hardware: All All
: P1 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords: target-2.0.0
Depends on:
Blocks:
 
Reported: 2013-02-06 14:27 UTC by (disabled) Jørgen Tjernø
Modified: 2013-07-27 03:11 UTC (History)
0 users

See Also:


Attachments
'hg import'-compatible patch that fixes this issue (13.24 KB, patch)
2013-02-06 17:30 UTC, (disabled) Jørgen Tjernø
Details | Diff
'hg import'-compatible patch that fixes this issue (13.23 KB, patch)
2013-02-06 19:08 UTC, (disabled) Jørgen Tjernø
Details | Diff
'hg import'-compatible patch that fixes this issue (20.43 KB, patch)
2013-02-08 10:50 UTC, (disabled) Jørgen Tjernø
Details | Diff
'hg import'-compatible patch that fixes this issue (20.46 KB, patch)
2013-02-08 11:06 UTC, (disabled) Jørgen Tjernø
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description (disabled) Jørgen Tjernø 2013-02-06 14:27:06 UTC
After SDL_Init finishes initializing the subsystems, it resets all the refcounts to zero. This means that the following causes weird behavior:

  SDL_Init(SDL_INIT_JOYSTICK);
  SDL_QuitSubSystem(SDL_INIT_JOYSTICK);
  SDL_InitSubSystem(SDL_INIT_JOYSTICK);

The joystick subsystem is *not* shut down by SDL_QuitSubSystem (because the refcount 

In addition, when SDL_Quit runs, it does not clear SDL_initialized - but it does clear SDL_SubsystemRefCount.
Comment 1 (disabled) Jørgen Tjernø 2013-02-06 14:28:21 UTC
Sorry, the paragraph after the example code was supposed to read:
"The joystick subsystem is *not* shut down by SDL_QuitSubSystem (because the refcount underflows when you run QuitSubSystem)."
Comment 2 (disabled) Jørgen Tjernø 2013-02-06 17:30:03 UTC
Created attachment 1034 [details]
'hg import'-compatible patch that fixes this issue
Comment 3 (disabled) Jørgen Tjernø 2013-02-06 19:08:22 UTC
Created attachment 1035 [details]
'hg import'-compatible patch that fixes this issue

Left an #include <stdio.h> in there from debugging.
Comment 4 Sam Lantinga 2013-02-07 21:15:24 UTC
The unit tests are a nice touch. :)

A couple of style suggestions:
* msb32_idx() isn't a portable function.  Maybe you should add something to SDL_stdinc.h?
* How about moving the incr and decr functions together so they're easy to see in one place?
Comment 5 (disabled) Jørgen Tjernø 2013-02-08 10:50:26 UTC
Created attachment 1036 [details]
'hg import'-compatible patch that fixes this issue

I moved msb32_idx into SDL_bits.h, and named it SDL_MostSignificantBitIndex.\
I also fixed a bug with SDL_AudioInit in the new system (it was deiniting itself on init, because we updated the WasInit state before invoking the Init function).

SDL_WasInit(SDL_INIT_AUDIO) is an invariant in SDL_AudioInit -- always false -- because we don't call SDL_AudioInit from InitSubSystem unless WasInit(SDL_INIT_AUDIO) was false - so I removed this call.

I also changed it so that we only increment refcount (and change WasInit state) of a subsystem *after* the Init function returns success. This fixes a problem where WasInit() would return true even if a subsystem failed to initialize. It also restores previous behavior of only changing WasInit state *after* init is done.
Comment 6 (disabled) Jørgen Tjernø 2013-02-08 11:06:34 UTC
Created attachment 1037 [details]
'hg import'-compatible patch that fixes this issue
Comment 7 (disabled) Jørgen Tjernø 2013-02-08 12:41:46 UTC
After talking to Sam in person:

When the patch is merged, skip the changes to src/audio/SDL_audio.c.
Comment 8 Ryan C. Gordon 2013-07-12 18:53:00 UTC
(Sorry if you get a lot of copies of this email, we're touching dozens of bug reports right now.)

Tagging a bunch of bugs as target-2.0.0, Priority 1.

This means we're in the final stretch for an official SDL 2.0.0 release! These are the bugs we really want to fix before shipping if humanly possible.

That being said, we don't promise to fix them because of this tag, we just want to make sure we don't forget to deal with them before we bless a final 2.0.0 release, and generally be organized about what we're aiming to ship.

Hopefully you'll hear more about this bug soon. If you have more information (including "this got fixed at some point, nevermind"), we would love to have you come add more information to the bug report when you have a moment.

Thanks!
--ryan.
Comment 9 Sam Lantinga 2013-07-27 03:11:27 UTC
This was committed by Jorgen in revision:
http://hg.libsdl.org/SDL/rev/e74a4b282450