I occasionally get Questions From Friends over mail or AIM or IRC. Usually they’re simple things and quick to answer. I figured I’d put the answers here too in case anyone’s really bored on a rainy sunday afternoon.
besides autoreleasing the memory, what is the difference between the following two statements?
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; NSMutableDictionary *dict = [NSMutableDictionary dictionary];
why would I ever NOT want to autorelease? Seems like the 2nd statement is easier
If you’re going to be immediately hanging on to it, the autorelease is wasted work,
or if you’re doing it inside of a tight loop – it’s better to get rid of it immediately rather than let a bunch of stuff accumulate in an autorelease pool. Of course, you can make and drain your own pool inside of your loop. Autorelease pools are pretty fast. But if you have a big spike of memory, say by accumulated autoreleased objects waiting for the grim reaper, that’ll push out things like mapped-in code segments, which may need to be brought back in later on. And on the phone, cycles count again, so it’s a hair more efficient to avoid autorelease. Just make sure you don’t accidentally forget to release it when you’re done.