Thread: Memory management question
06-16-2009, 12:03 PM #1
Memory management question
- Member Since
- Jun 16, 2009
Having coded for many years in everything from Python to 6502 assembler, I'm finally learning Objective C.
Obviously, memory management is the big 'huh?' at the moment, so I was wondering if anyone had time to help me understand what's going on in the little code snippet below.
Given a class 'Head', and a class 'Human' defined elsewhere, I tried allocating a new Head for the Human in two different ways:
1) this is the way I like to code, but it leaves a permanently inflated retain count (as far as I can tell from my NSLog-ing):
human.head = [[Head alloc] init];
2) whereas this does not:
Head *head = [[Head alloc] init];
human.head = head;
I'm well aware that there was no 'release' called in 1), but it seems odd if a one-line creation/assignment of this sort isn't treated as an auto-release.
Many thanks - especially if this is a really stupid question.
06-18-2009, 02:46 PM #2
- Member Since
- Jun 25, 2005
- On the road
- 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
The class methods alloc, copy, and new, create objects with a retain count of 1, not set for autorelease. In those cases, and where you explicitly perform a retain, you are responsible for performing a balanced release on an object. Since case one has no balanced release, you are leaking memory.
In case two you have balanced the retain count with a release, so things look good. I think though that if you refer to human.head after releasing head, you can end up crashing. The reason being that you simply assigned the ID (a pointer) from head to human head and they are both looking at the same object.
I've watched all of the Stanford iPhone classes that you can download via iTunes U and the memory discussion of interest to this question is class 3. The "Object Life Cycle" section starts at minute 19 at slide 18, which are also downloadable. Except for the specific iPhone items, the class is very applicable to OS X coding too.
When you receive an object via other means other than alloc, copy, and new, you are receiving and autorelease object. I don't know if there are any exceptions to this rule. If you want to keep it, you need to retain it, or copy it.
Apple has some information on the new garbage collection that arrived with Leopard; Objective-C 2.0 Overview, Introduction to Garbage Collection. As I understand it, you wouldn't have to worry about the retain stuff. I've also heard that there are or were some issues with some frameworks.CameraTime - Time lapse photography for novice and advanced users.
When asking questions, post the version of your software. You'll receive better answers.
Please post your results to the thread as it is good feedback.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
By EightmanVT in forum OS X - Operating SystemReplies: 8Last Post: 02-15-2011, 04:53 PM
By naltoidaddict in forum OS X - Operating SystemReplies: 3Last Post: 04-06-2008, 02:44 PM
By nownot in forum OS X - Operating SystemReplies: 2Last Post: 01-22-2007, 02:58 PM
By munkism in forum OS X - Operating SystemReplies: 3Last Post: 04-22-2006, 03:31 PM
By rs2sensen in forum OS X - Operating SystemReplies: 0Last Post: 11-01-2005, 11:44 PM