views:

172

answers:

2

Here is my question.

NSString *xyz=[NSString stringWithFormat:@"%i %@",10,@"Sagar"];

Now I am taking other string, as follows.

NSString *x2=[xyz copy];

I exactly don't know what will happen here?
( Is it something like, x2 has the ref of xyz's ref. )

NSString *x3=[xyz retain];

( What will happen here, x3 has a new memory having copied string or [xyz copy] does that? )

Now, how to remove all these three strings from memory?

Thanks in advance.

sagar.

+5  A: 
NSString *xyz=[NSString stringWithFormat:@"%i %@",10,@"Sagar"];

This will create autoreleased instance of NSString - it will be released when the autorelease pool is drained (typically on the next run loop).

NSString *x2 = [xyz copy];

In theory -copy message will create a new instance of the object with retain count 1 (that is you must release it somewhere), but as NSString object is immutable then [xyz copy] will be optimized to [xyz retain] and thus it will point to the same instance.

NSString *x3=[xyz retain];

x3 will point to the same instance as xyz (and x2), and its retain count will be incremented - you must release your object somewhere.

Now, how to remove all these three strings from memory?

Make sure that you pair all retain (copy) messages with release and memory will be freed.
Read Objective-c memory management guide for more details.

Vladimir
An `autoreleased` object will be released the next time the pool it was added to is drained. For applications with run loops, the application's main pool is typically drained at the next iteration of the run loop.
dreamlax
Yes dreamlax, it seems I need to revise memory management docs myself :)
Vladimir
Hehehe :), a fine answer otherwise; have an upvote.
dreamlax
@Vladimir I can't agree with your last paragraph. The string [xyz/x3] should be released once, and the string x2 should be released once. Even though the implementation of the library you're using happens to make them the same instance, your code should work to the assumption that x2 is a copy of the object, not a reference. One day your "copy==same object" assumption will break.
Graham Lee
@Graham Lee. agree with you, removed that
Vladimir
+2  A: 

In situation like this it is especially helpful to familiarize yourself with the message naming conventions/rules associated with memory management in objective-c and cocoa (and related frameworks):

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it. (Memory Management Programming Guide for Cocoa)

consequently, you can assume, that every object that you ever receive from a message that is not named according to the scheme laid out above is either autoreleased or taken care of by some other means (it may be a shared object managed by some other object etc.)

If you just keep this in mind, your questions can be answered quickly:

  1. You receive the NSString *xyz from a message whose name does not match the scheme described in the rule above (not alloc, not new, not copy, not retain). You must not release it.

  2. You receive the NSString *x2 from a message named copy. You must release it

  3. You receive the NSString *x3 from a message named retain. You must release it.

VoidPointer