New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

Memory management question


Post Reply New Thread Subscribe

 
Thread Tools
Madrayken

 
Member Since: Jun 16, 2009
Posts: 1
Madrayken is on a distinguished road

Madrayken is offline
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;
[head release];


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.
QUOTE Thanks
xstep

 
xstep's Avatar
 
Member Since: Jun 25, 2005
Location: On the road
Posts: 3,231
xstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to allxstep is a name known to all
Mac Specs: 2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid

xstep is offline
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.
QUOTE Thanks

Post Reply New Thread Subscribe


« Releasing memory try-catch block statement in Objective-C | Help With Coding... »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Will I see an improvement upgrading to 4gb of RAM over 2gb miketrue Apple Notebooks 9 08-22-2008 06:55 AM
Memory Question (Using PC Memory in Mac Pro) dj_hype Switcher Hangout 4 12-17-2007 05:14 PM
pls explain memory management nownot OS X - Operating System 2 01-22-2007 01:58 PM
Memory question... Willy Apple Desktops 0 11-14-2006 04:16 PM
Power PC G3 Memory question Malibu Other Hardware and Peripherals 1 10-13-2003 08:54 AM

All times are GMT -4. The time now is 11:54 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?