Autorelease iPhone
Asked Answered
M

6

2

Coming up towards the end of developing an iPhone application and I'm wondering just how bad is it to use autorelease when developing for the iphone. I'm faced with some fairly random crashes and, so far, I can't pinpoint it to anything other than sloppy memory usage.

As a Cocoa newbie I remember initially reading a guideline document that strongly suggested avoiding autorelease in favor of manual retain/release for iPhone. However, a more 'senior' Cocoa developer came on board early on (who ironically has been let go since), who used autorelease all over the place. Admittedly, I was went into "monkey see monkey do" mode, and it appears to be coming back to haunt me (I'm now the only developer on the project).

So what to do next? It seems to me that I have to branch the code and try to go through and replace, where possible, autorelease code keeping my fingers crossed that I don't inadvertently break the app. It seems a lot of library calls result in autoreleased objects like stringWithFormat and pretty much anything where I'm not using alloc myself. Any other gotchyas and/or suggestions I should be looking out for? Thanks Cocoa gurus.

Massimo answered 9/1, 2009 at 20:9 Comment(0)
I
12

Using release instead of autorelease can improve memory usage in tight spots (which is good on the iPhone), but it's not going to help at all with crashing if you're not following the retain / release rules. I would read a few tutorials on memory management in Obj-C if you're still a little hazy on what you should be doing, and then go after those crashes using the debugger and crash reports to find out where you're over releasing objects. This and this are two good places to start.

Intermigration answered 9/1, 2009 at 20:14 Comment(1)
"This" links are dead.Hoem
S
4

More important than the autorelease or manual-release choice is how often you alloc and dealloc your NSAutoreleasePools. Since most of the Cocoa frameworks use autorelease liberally, you need to have a proper pool draining strategy. Once that is in place, the choice of whether to release or autorelease becomes much less an issue.

That being said, the only areas you should worry about are tight loops--allocate and release an NSAutoreleasePool every few iterations for best results; and when you have spawned another NSThread that doesn't have a Runloop--create a pool and drain it every so often becomes idle. Since most applications only allocate a small amount of data per event, UIKit's strategy of allocating the pool before the event is dispatched and releasing it after the dispatch returns works very well.

Sabayon answered 10/1, 2009 at 10:39 Comment(0)
I
2

If you think you dunno how to use autorelease, check out CS193p FALL 2010 on iTunes U -> Lecture number 4.

It teaches you all about memory management and stuff (if you skip the first 10 minutes or so)

Infecund answered 20/11, 2010 at 5:10 Comment(0)
R
0

For iPhone performance reasons, Apple suggest that, whenever possible, you shouldn't use autoreleased objects. Instead, explicitly release your objects when you are done with them.

Radack answered 9/1, 2009 at 20:47 Comment(2)
I have no reference. This was something suggested in one of their Tech Talks. Basically, when you autorelease an object, it hangs around for an arbitrary period of time until the system can destroy it. This means that while it may be "gone," it's still using resources -- something very valuable on the iPhone. By explicitly releasing objects, you get those resources back immediately.Radack
Look in the following document Memory Usage and Performance Guidelines Search for "Release Unused Memory"Noni
R
0

Using autorelease pools means that you might be leaving some unused memory lying around. Since the iPhone has less memory to go around, you might improve performance if you free up unneeded memory as soon as possible, rather than letting it sit around taking up resources while it waits for an autorelease.

Rumelia answered 27/1, 2010 at 3:24 Comment(0)
O
0

When you autorelease, you're basically saying: "I don't need this any longer, but anyone else is free to pick it up (before the auto release pool is drained)". When you explicitly relase an object you're saying: "I don't need this any longer and unless anyone else has already said otherwise (acquired), it should be deallocated immediately."

Consequently, autorelease is not normally the wrong thing to. It is required when you want to pass objects back to the sender of a message without requiring the sender to take care of releasing the object.

Oligopsony answered 8/11, 2012 at 7:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.