views:

49

answers:

2

Here is the code:

-(void)myOwnMethod{


    NSString *myString;

    myString = [[NSString alloc]init];

    /*

    Some logic about the String


    */

    [myString release]; //Do I need to release the myString Object?

}

As you can see, the myString object is only used in the method, do I need to release it? or it will auto release when the method finish? Thank you.

+6  A: 

Yes, you need to release it. If you called alloc, you must call either release or autorelease.

Note: If you are using garbage collection (currently only available on OS X, not iOS), then release and autorelease do nothing, and you do not need to call them. If you are writing memory-managed code (which includes anything written for iOS), then you must call them.

BJ Homer
+1  A: 

Yes. Simple rule for every alloc, retain, or copy, you need a release or autorelease.

This goes for both Objective-C and CoreFoundation objects.

jojaba