Autorelease/Retain/Release
Autorelease/Retain/Release
- Subject: Autorelease/Retain/Release
- From: Tim Davis <email@hidden>
- Date: Tue, 24 Jul 2007 21:49:44 -0400
First off let me apologize for asking such a stupid question, I know
to the list it may seem dumb, but I'm just a little confused. I come
from a Windows .NET Framework background and switched to Apple a few
years ago and finally decided to learn Apple software instead of
wasting time trying to write .NET apps on Apple.
Anyways, my question is about Autorelease Pools. In the Cocoa Docs
it states the following:
autorelease
Decreases the reference count of an object by 1 at some stage in the
future
Relinquishes ownership of an object at some stage in the future.
The following rules apply:
Within a given block of code, the number of times you use copy, alloc
and retain should equal the number of times you use release and
autorelease.
You don’t own objects created using convenience constructors (such as
NSString's stringWithString:) or any other accessor method, so
(unless you send them a retain message) you do not have to release them.
Implement a dealloc method to release the instance variables you own.
You should never invoke dealloc directly (other than when you invoke
super’s implementation in a custom dealloc method).
Ok, so I understand the first bullet, and the third and fourth. But
the second bullet has me confused. How do I know what is an accessor
method/convenience method?
Does this mean that if I don't send the object a [retain] call during
the [[blah alloc] init] statement that I don't need to send it a
[release] later on? Yes I have read the docs, just unsure about it.
In .NET it was all handle automagicly, but Apple's approach seems to
make more sense. If I want an object to hang around I [retain] it,
when I want it to go to the trash can I [release] it.
Here's my code:
if (![fm fileExistsAtPath: [path stringByAppendingString: @"/
index.xml"]])
{
//Create Minimal XML file for use later.
//Create root element with tag: Root
NSXMLElement *indexRoot = [[[NSXMLElement alloc] initWithName:
@"Root"] autorelease];
[indexRoot addChild: [NSXMLNode elementWithName: @"Namespaces"]];
//Create the document and assign the root element
NSXMLDocument *indexFile = [[[NSXMLDocument alloc]
initWithRootElement: indexRoot] autorelease];
//Grab the raw XML data from the document so we can save it
NSData *xmlData = [indexFile XMLDataWithOptions:
NSXMLNodePrettyPrint];
//Save the data to a file.
[xmlData writeToFile: [path stringByAppendingString: @"/index.xml"]
atomically: YES];
}
else
{
//Load Xml File
}
[pool release];
I created an autorelease pool farther up the awakeFromNib function.
Please look at indexRoot, indexFile, and xmlData. If I understand
this correctly I need to use autorelease on indexRoot and indexFile
because I'm alloc'ing and init'ing? And xmlData is automagicly
destroyed when awakeFromNib exits?
Thanks for your time and help.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden