views:

258

answers:

1

I have a simple application that counts down to midnight and then plays a beeping sound over and over again and blinks the label showing the time. So the label blinking uses an NSTimer, while the beeping goes on its own Thread, so that the pure C function (for the system sound id completion callback) can use [NSThread sleepForTimeInterval:...]; to wait a bit in between playing the alarm beep sound. So I get two error messages during execution saying theres no autorelease pool and it will just leak. Its because in the method that I call into a new thread, There's two @"Strings" that are used to create the System Sound ID (the file name and extension). Is there a way I can not use an autorelease pool since those are the only two things that ever try to use it. Thanks.

Or should I use two NSTimers instead of another thread..?

Ben Gottlieb's answer was right, however I decided to use another NSTimer instead because I forgot about how its not really synced to the time when you sleep the thread as the actual execution of the code before it takes a bit of time. And it makes things symmetrical ;).

+2  A: 
Ben Gottlieb
what's the difference between that and `[pool drain];` ?
Mk12
The `-drain` method is the newer preferred method, which also plays well with garbage collection. The method is available on 10.4+, I believe.
Quinn Taylor
I'm on iphone, not OS X.
Mk12
-drain works fine on iPhone, and it's a good habit to get into.
Rob Napier
How come the automatically generated `main.m` in all projects uses `[pool release];` ? And how does the current thread know to use this Autorelease pool that we could have named anything?
Mk12
Also how do NSTimer's manage to only use the current thread? I tested some `[NSThread isMainThread];` statements and they were always true, even in the timer method. Also, i noticed whenever I call `[NSThread isMultiThreaded];` it returns true. Is this just because its in the simulator?
Mk12