Objective-C memory management (alloc and autorelease)

When you select and initialize objects, and then want to return that object, how should you return it?

I have the following code:

NSXMLDocument* fmdoc = [[NSXMLDocument alloc] initWithContentsOfURL:trackInfoUrl
    options:NSXMLDocumentTidyXML error:&err];  
return [fmdoc autorelease];

Is it correct?

+5
source share
1 answer

It is right. Since you initialize the object, you are responsible for its release or auto-advertisement.

Since the save value for creation is 1, and you want it to not be deleted before the calling method has the ability to use the object, auto-detection is the right message to send.

, . autorelease, ​​ , , , , .

+4

All Articles