views:

134

answers:

2
NSArray     *path                   = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString    *documentsDirectory     = [path objectAtIndex:0];

    NSString    *databasePath           = [documentsDirectory stringByAppendingPathComponent:@"DB"];
    NSString    *fileName               = [newWordbookName stringByAppendingString:@".csv"];
    NSString    *fullPath               = [databasePath stringByAppendingPathComponent:fileName];

    [[NSFileManager defaultManager] createFileAtPath:fullPath contents:nil attributes:nil]; 

    [databasePath release];
    //[fileName release]; Error!
    //[fullPath release]; Error!

    //NSLog(@"#1 :databasePath: %d",[databasePath retainCount]);
    //NSLog(@"#1 :fileName: %d",[fileName retainCount]);
    //NSLog(@"#1 :fullPath: %d",[fullPath retainCount]);

Hi guys, I'm using this code and want to release NSString* .. so, I declare fileName, fullPath, and databasePath of NSString. database is released but fileName, fullpath doen't release. I don't know why it happen.

I know that NSArray is Autoreleased. But Is documentsDirectory autoreleased? (newWordbookName is nsstring type)

I hope that I look through a document about iPhone memory management. Please advice for me.

+2  A: 

By convention the only two cases when a method returns a retained object are constructors i.e. alloc, new etc. and object-copying methods (containing copy in their name).

In all other cases the object is expected to be autoreleased, unless explicitly stated otherwise in the documentation.

This is the complete memory management documentation: Cocoa Memory Management

Plamen Dragozov
+2  A: 

You should not be calling release on any of the objects in the above code.

The reason the NSArray is autorelease'd is the same reason all the other objects are autorelease'd: the methods that assigned them their values called autorelease on them before they returned. In general, you can assume methods return autorelease'd objects if they do not have the word "create" or "new" in them. That is the general Cocoa convention. (Although 3rd party code may be goofy and do things differently, so caveat programmer).

You only really need to worry about objects you alloc or copy yourself; in other words, pair every alloc or copy with a release or autorelease.

Shaggy Frog