Array of CFURLRefs in a struct

Discussion in 'iOS Programming' started by neil.b, Dec 12, 2008.

  1. macrumors member


    Nov 20, 2008
    I'm trying to define a struct to create a simple sound object that contains a list of sound files and some flags;

    typdef struct {
            BOOL playing;
            BOOL monophonic;
    } SimpleSoundStructure;
    SimpleSoundStructure mySimpleSound;
    -(void) initSimpleSoundStructure
            NSArray *sampleFileNames = [NSArray arrayWithObjects:
            NSString *aSampleFile;
            for (int i = 0; i < NUMBER_OF_SOUNDS_IN_SET; i++)
               mySimpleSound.soundURL[i] = (CFURLRef)[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:aSampleFile ofType:NULL]];
    -(void) playASound:(int)sound
            AudioServicesCreateSystemSoundID (mySimpleSound.soundURL[sound], &mySoundSet.soundObject[sound]);
    But I always get a "BAD ACCESS" error (and program termination) when I try to call the AudioServiceCreateSystemSoundID function in the playASound function.

    What is weird is that if I NSLog mySimpleSound.soundURL in the for...loop in the init function, it has the correct path to the sample files. But if I do the same in the playASound function, the log shows the path to be something like "UI...white space" (sorry, I've changed the function so can't remember the exact error).

    What's even weirder, if I put a AudioServiceCreateSystemSoundID call in the init for...loop, when I retrieve the path in the playASound function the path to the sample is now correct and the function works.

    Pulling my hair out on this one :(
  2. macrumors newbie

    Aug 30, 2007
    My first guess is that you need to retain the NSURL returned from [NSURL fileURLWithPath:]. There's a very good chance the NSURL is being autoreleased somewhere between your init method and your playASound method.

    This also means that you need to remember to release these NSURLs at some point as well.
  3. thread starter macrumors member


    Nov 20, 2008
    Spot on, thank you very much! :D

    Now to come up with a smart way of releasing the NSURLs.... :)

Share This Page