Amazon.com Widgets Do we always need to alloc/init or call a method on ex. NSString
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 24, 2013, 09:44:38 AM
Home Help Search chat Login Register   
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+  Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+  Old Stuff
| |-+  Chapter Study
| | |-+  Chapter 15 - Numbers, Strings, and Collections
| | | |-+  Do we always need to alloc/init or call a method on ex. NSString
Pages: [1]   Go Down
Print
Author Topic: Do we always need to alloc/init or call a method on ex. NSString  (Read 978 times)
jodles
Newbie
*
Posts: 23






« on: August 12, 2009, 01:55:25 PM »

Hi!

Just something bothering me, that I can't find the answer of.
When creating a new NSString, or NSArray, etc., you would usually do:
NSString *myString = [NSString string]; (autorelease)

which would return an empty NSString object.
Or alternatively
NSString *myString = [[NSString alloc] init];
then releasing it later.

Now, what if I didn't alloc or called the string method on NSString, just did this for example:

NSString *myString = @"~";

it works without giving me any errors or warnings, but is there something fundamentally wrong here?
No object is returned, so how can it be assigned  a string? (@"~"). And how is it released now? Is this a leak? How would we release this? Why do this work at all? Haha.

Pretty basic stuff this.. I just want to make sure I have understood such basics right.

Pardon my ignorance!
Joachim
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #1 on: August 12, 2009, 02:51:19 PM »

That is because @"~" is a constant that is allocated and initialized when your program starts up. The compiler makes sure this happens.

You don't need to release it. It will be released when your program quits. In fact... you can't release it (it's a constant after all).

The compiler is smart enough to know the every time you assign @"~" to an NSString object it will use the same constant. So only one instance of @"~" will exist.

And FYI. [NSString string] returns a pointer to an empty string constant  Grin

This is covered in chapter 17. The following code is similar to program 17.2 except that I attempt to release a constant string object. Don't worry, your computer won't burst into flames Grin

Code: (Objective-C)
#import <Foundation/Foundation.h>

int main (int argc, const char * argv[])
{
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

  NSString  *constantString = @"~";
  NSLog(@"constantString retain count: 0x%X", [constantString retainCount]);
  [constantString release];
  NSLog(@"constantString released");
  NSLog(@"constantString retain count: 0x%X\n\n", [constantString retainCount]);
    
  NSString *emptyString = [NSString string];
  NSLog(@"emptyString retain count: 0x%X", [emptyString retainCount]);
  [emptyString release];
  NSLog(@"emptyString released");
  NSLog(@"emptyString retain count: 0x%X\n\n", [emptyString retainCount]);
  
  NSMutableString *string = [NSMutableString stringWithString:@"~"];
  [string retain];
  NSLog(@"string retain count: 0x%X", [string retainCount]);
  [string release];
  NSLog(@"string released");
  NSLog(@"string retain count: 0x%X", [string retainCount]);
  
  [pool drain];
  return 0;
}

« Last Edit: August 12, 2009, 03:09:10 PM by rgronlie » Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
jodles
Newbie
*
Posts: 23






« Reply #2 on: August 13, 2009, 12:48:02 AM »

Thank you, rgronlie!

That cleared up a lot:)

One last thing, if I did
NSString *string = [[NSString alloc] init]; (1)

then I would have to release it, right? [string release]; (2)

Everytime I call alloc myself I should at some point release?
But in what situation would I want to do (1), and not just NSString *string;?

Based on the info here: http://cocoadevcentral.com/d/learn_objectivec/ (under "creating objects"), it seems as if I want to use autorelease, I would do:
NSString *string = [NSString string]; (3)

and if I wanted to control it myself:
NSString *string = [[NSString alloc] init];  ----> [string release]; (4)

When I assign a string, @"~", directly; NSString *string = @"~";, you said that it will be released when the program quits. Would that mean that it would be equivalent to doing (3) first? I.e. it's being autoreleased?

Wow, my thinking is garbled today... hope it made any sense:p

Joachim
Logged
rgronlie
Global Moderator
Full Member
*****
Posts: 212







« Reply #3 on: August 13, 2009, 09:50:22 AM »

Hi,

Rule is:

Any object created by alloc, new, copy or mutableCopy  are not in the autorelease pool so you must release them or autorelease them.

Objects created using any other method are assumed to be in the autorelease pool.

Constants (i.e. @"~") are not in the autorelease pool. They are released when your program ends.

Autoreleased objects are released every time your program runs through it's event loop. This may be a bit confusing since the programs we've been writing here don't have an event loop and the autorelease has been occurring just prior to the program ending. Your first taste of an event loop will be in Part III of the book.

Ryan
Logged

Sanity: Minds are like parachutes. Just because you've lost yours doesn't mean you can borrow mine.
skochan
Administrator
Hero Member
*****
Posts: 3104







« Reply #4 on: August 13, 2009, 09:58:40 PM »

Thanks for the replies Ryan.  I'm on vacation for a week, so I can tell the forum will be in good hands while I'm gone.  Cool

Cheers,

Steve
Logged
Pages: [1]   Go Up
Print
Jump to:  



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.