Results 1 to 4 of 4

Thread: CL_SoundOutput unguarded singleton, can crash if deleted/re-constructed?

  1. #1

    Default CL_SoundOutput unguarded singleton, can crash if deleted/re-constructed?

    I just debugged a crash associated with CL_SoundOutput. I had a CL_SoundOutput as member data in an object that got constructed and deleted multiple times. Always on the 2nd time I'd get a bad impl in the CL_SoundOutput, causing a crash, usually in the mutex code, as the CriticalSection was bogus.

    I reread http://www.clanlib.org/beta/index.php/MainDocs:Sound and saw that CL_SoundOutput is supposed to be a singleton...I reimplemented so that I only instantiate 1 CL_SoundOutput and use it across the app.

    Would you expect such a crash? If so, should CL_SoundOutput guard against it?

    I can (in theory) reproduce the crash in a test case.

  2. #2
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    You are right CL_SoundOutput is supposed to be a singleton.

    I'm not sure how to guard against it.

    But since CL_SoundOutput is a singleton, could the implementation be moved into CL_SetupSound ?

    But CL_SetupSound, CL_SetupCore, CL_SetupDisplay probably have no singleton guards.

    I do not have an answer.

    (Note, A PC usually only has a single operating sound card. It is unlikely someone would want to use 2 sound cards at the same time)

  3. #3
    ClanLib Developer
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    554

    Default

    It is easy to guard against creating a singleton twice. Simply throw an exception in CL_SoundOutput's constructor if the static instance pointer already has an object assigned.

    I.e.

    Code:
    class MySingleton
    {
    public:
      MySingleton()
      {
        CL_MutexSection lock(&mutex);
        if (instance) throw CL_Exception("Sorry can't do that");
        instance = this;
      }
    
      ~MySingleton()
      {
        CL_MutexSection lock(&mutex);
        instance = 0;
      }
    
    private:
      static CL_Mutex mutex;
      static MySingleton *instance = 0;
    };

  4. #4
    ClanLib Developer
    Join Date
    May 2007
    Posts
    1,824

    Default

    Commited the singleton check to svn

Similar Threads

  1. CRASH
    By Hawk in forum Funeral Quest
    Replies: 4
    Last Post: 04-18-2010, 03:11 PM
  2. Crash this bug
    By huntercool in forum Official ClanLib SDK Forums
    Replies: 2
    Last Post: 09-29-2009, 02:40 PM
  3. Crash
    By valdirSalgueiro in forum Official ClanLib SDK Forums
    Replies: 4
    Last Post: 03-14-2009, 01:47 AM
  4. Replies: 1
    Last Post: 03-29-2004, 05:01 AM
  5. my account got deleted?
    By in forum Funeral Quest
    Replies: 6
    Last Post: 05-25-2003, 01:09 AM

Bookmarks

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •