21 ms·
I think that if you master UIView and MVC as Apple prescribes it, you learn the conventions you need for proper memory management in ARC. Knowing the convention
by AshFurrow 15y ago
I think that if you master UIView and MVC as Apple prescribes it, you learn the conventions you need for proper memory management in ARC. Knowing the conventions is going to suffice.
My argument wasn't to go and be naive about memory management. It was to not worry about it right now. Memory management makes the already steep learning curve of Objective-C even steeper, and can scare away newcomers.
- Hisoka 15y agoI spent the first month struggling with memory management, and kept being paranoid about "autorelease". Now, I ONLY use autorelease (well in 99% of cases). I don't get why so many people say be careful about autorelease. It makes things so much simpler and prevents mistakes from not releasing after allocating. I wish I learned this much earlier.
- DrJokepu 15y agoWell, be careful with that. A lot of properties in Cocoa and Cocoa Touch are not retained automatically (delegates, data sources and so on). If you just autorelease them you will have problems. The other reason people are careful about autoreleases is because, obviously, objects in the autorelease pool won't get deallocated until the pool is drained, which normally happens in the main application loop. Normally, this is not a problem, but if you do memory-intensive things (like dealing with images or other media) in a loop that means that you will consume a lot of memory before the autorelease pool gets drained. This might get other apps killed (on iOS 4+), and ultimately, yours too.
- danudey 15y agoThe other problem with autorelease is when you release too many times; the autorelease pool gets drained down the road, and your app crashes in some other situation (inconsistently) at a time unrelated to when you made the mistake.
- Aqua_Geek 15y agoAutorelease isn't a silver bullet for Obj-C memory management. There are a number of cases in which you would NOT want to autorelease objects - tight loops, for example. If you're already in the habit of autoreleasing everything you +alloc, why not just release it when you're done with it instead?
- Hisoka 15y agobecause like I said it's easy to forget when to do that. Example UIView* view = [[UIView alloc] init]; ...do a bunch of stuff [view release] <-- you have to remember this vs. UIView* view = [[[UIView alloc] init] autorelease]; Of course I know about the cases where we have tight loops,w hich is why i said 99 of the time.
- marcomonteiro 15y agoPersonally I only use autorelease when I create an object I'm not responsible for. For example, if I create a view somewhere for another object to utilize. I also like knowing that as soon as I'm done with an object that it gets killed and the system can reclaim the memory.
- bignoggins 15y agowhen you autorelease, the reference goes into an autorelease pool that is drained later. So you will likely end up using much more memory than you need to. Since iOS runs on memory constrained devices, this could mean the difference between running smoothly and crashing due to memory warnings all the time.