Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 09-20-2010, 07:38 PM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 92
termitis is on a distinguished road
Default String alloc question! REALLY NEED HELP

Hi Im new at iPhone SDK and i have found these lines in the book "Beginning iPhone 3 development":

NSString *title =[sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:@"%@buttonpressed!",title];
statusText.text =newText; (statusText is UILabel *statusText; and is placed inside @interface...its not allocated)
[newText release];

my questions:
-Ive learnd to create strings like that: NSString *str=@"example";
this isnt allocated..so why is newText allocated?
-title isnt a string of *str type?it isnt allocated right?
-when I release newText is statusText.text released too?If yes,then why is [statusText release]; later?statusText was never allocated at @interface..

thanks alot for your help
termitis is offline   Reply With Quote
Old 09-20-2010, 08:54 PM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by termitis View Post
Hi Im new at iPhone SDK and i have found these lines in the book "Beginning iPhone 3 development":

NSString *title =[sender titleForState:UIControlStateNormal];
NSString *newText = [[NSString alloc] initWithFormat:@"%@buttonpressed!",title];
statusText.text =newText; (statusText is UILabel *statusText; and is placed inside @interface...its not allocated)
[newText release];

my questions:
-Ive learned to create strings like that: NSString *str=@"example";
this isnt allocated..so why is newText allocated?
-title isnt a string of *str type?it isnt allocated right?
-when I release newText is statusText.text released too?If yes,then why is [statusText release]; later?statusText was never allocated at @interface..

thanks alot for your help

Q:-Ive learnd to create strings like that: NSString *str=@"example";

A: That is creating a constant string. Sort of like static char* foo = "foobar"; in c.

Q: this isnt allocated..so why is newText allocated?

A: newText isn't a static string. It's a string created by calling the NSString method initWithFormat.

Q: -title isnt a string of *str type?it isnt allocated right?

A: title is a string that's returned by calling a button's "senderForState" method. That method returns an autoreleased object. It was allocated by the caller, and then sent an "autorelease" message. The autorelease method tells an object that it should be released the next time the app visits the event loop. You use autoreleased objects as throw-aways. They persist until the last call in the call chain that created them returns, and the system drops back to the main event loop. At that point, an object called the "autorelease pool" gets "drained", which means that all objects that were sent autorelease messages get a release message for every autorelease they got in the last pass through your code. If any object's retain count drops to 0 while draining the autorelease pool, it gets deallocated.

There is a strong convention in Cocoa. If a method name returns an object and it's name starts with "new" or "alloc", or contains "copy", you own it, and are responsible for releasing it when you are done with it. All others return objects that are either still owned by the called object, or have been autoreleased, and will be discarded if you don't retain them.

Q:-when I release newText is statusText.text released too?


A: No. statusText is a probably a UILabel object. It has a "text" property that lets you assign strings to it. It's string property is almost certainly set up to make a copy of the string, which means that once you set statusText, you don't need newText any more, and can release it.

Q:If yes,then why is [statusText release]; later?statusText was never allocated at @interface..

The only time you would want to release statusText is if you retained it when you created it, and you were removing it from the view it was a part of. Usually UI objects are owned by their parent view "superview" and you don't have to do any memory housekeeping with them. When you release the top-most view of a view hierarchy, it releases it's subviews, which in turn release their subviews, causing the whole mess to be thrown away.

-----------------
You should stop what you are doing, go to XCode, search on "Memory Management Programming Guide", and read the entire document, think about it, then read it again from beginning to end. Perhaps even print it out and mark it up with a highlighter. It's probably the single most important thing you need to understand about programming in Cocoa, and until you get it, you'll write apps that crash unexpectedly, or leak like a sieve.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 09-21-2010, 03:14 AM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 92
termitis is on a distinguished road
Smile

Quote:
Originally Posted by Duncan C View Post
Q:-Ive learnd to create strings like that: NSString *str=@"example";

A: That is creating a constant string. Sort of like static char* foo = "foobar"; in c.

Q: this isnt allocated..so why is newText allocated?

A: newText isn't a static string. It's a string created by calling the NSString method initWithFormat.

Q: -title isnt a string of *str type?it isnt allocated right?

A: title is a string that's returned by calling a button's "senderForState" method. That method returns an autoreleased object. It was allocated by the caller, and then sent an "autorelease" message. The autorelease method tells an object that it should be released the next time the app visits the event loop. You use autoreleased objects as throw-aways. They persist until the last call in the call chain that created them returns, and the system drops back to the main event loop. At that point, an object called the "autorelease pool" gets "drained", which means that all objects that were sent autorelease messages get a release message for every autorelease they got in the last pass through your code. If any object's retain count drops to 0 while draining the autorelease pool, it gets deallocated.

There is a strong convention in Cocoa. If a method name returns an object and it's name starts with "new" or "alloc", or contains "copy", you own it, and are responsible for releasing it when you are done with it. All others return objects that are either still owned by the called object, or have been autoreleased, and will be discarded if you don't retain them.

Q:-when I release newText is statusText.text released too?


A: No. statusText is a probably a UILabel object. It has a "text" property that lets you assign strings to it. It's string property is almost certainly set up to make a copy of the string, which means that once you set statusText, you don't need newText any more, and can release it.

Q:If yes,then why is [statusText release]; later?statusText was never allocated at @interface..

The only time you would want to release statusText is if you retained it when you created it, and you were removing it from the view it was a part of. Usually UI objects are owned by their parent view "superview" and you don't have to do any memory housekeeping with them. When you release the top-most view of a view hierarchy, it releases it's subviews, which in turn release their subviews, causing the whole mess to be thrown away.

-----------------
You should stop what you are doing, go to XCode, search on "Memory Management Programming Guide", and read the entire document, think about it, then read it again from beginning to end. Perhaps even print it out and mark it up with a highlighter. It's probably the single most important thing you need to understand about programming in Cocoa, and until you get it, you'll write apps that crash unexpectedly, or leak like a sieve.
thank you so much for your time!! I appreciate it!!
termitis is offline   Reply With Quote
Old 09-21-2010, 03:26 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 92
termitis is on a distinguished road
Default

Quote:
Originally Posted by Duncan C View Post
Q:-Ive learnd to create strings like that: NSString *str=@"example";

A: That is creating a constant string. Sort of like static char* foo = "foobar"; in c.

Q: this isnt allocated..so why is newText allocated?

A: newText isn't a static string. It's a string created by calling the NSString method initWithFormat.

Q: -title isnt a string of *str type?it isnt allocated right?

A: title is a string that's returned by calling a button's "senderForState" method. That method returns an autoreleased object. It was allocated by the caller, and then sent an "autorelease" message. The autorelease method tells an object that it should be released the next time the app visits the event loop. You use autoreleased objects as throw-aways. They persist until the last call in the call chain that created them returns, and the system drops back to the main event loop. At that point, an object called the "autorelease pool" gets "drained", which means that all objects that were sent autorelease messages get a release message for every autorelease they got in the last pass through your code. If any object's retain count drops to 0 while draining the autorelease pool, it gets deallocated.

There is a strong convention in Cocoa. If a method name returns an object and it's name starts with "new" or "alloc", or contains "copy", you own it, and are responsible for releasing it when you are done with it. All others return objects that are either still owned by the called object, or have been autoreleased, and will be discarded if you don't retain them.

Q:-when I release newText is statusText.text released too?


A: No. statusText is a probably a UILabel object. It has a "text" property that lets you assign strings to it. It's string property is almost certainly set up to make a copy of the string, which means that once you set statusText, you don't need newText any more, and can release it.

Q:If yes,then why is [statusText release]; later?statusText was never allocated at @interface..

The only time you would want to release statusText is if you retained it when you created it, and you were removing it from the view it was a part of. Usually UI objects are owned by their parent view "superview" and you don't have to do any memory housekeeping with them. When you release the top-most view of a view hierarchy, it releases it's subviews, which in turn release their subviews, causing the whole mess to be thrown away.

-----------------
You should stop what you are doing, go to XCode, search on "Memory Management Programming Guide", and read the entire document, think about it, then read it again from beginning to end. Perhaps even print it out and mark it up with a highlighter. It's probably the single most important thing you need to understand about programming in Cocoa, and until you get it, you'll write apps that crash unexpectedly, or leak like a sieve.
one more thing...how do you know all these about what these methods return(autorelease for example)?is there an index where i can see too?
termitis is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 331
14 members and 317 guests
2ndSegment, cayladv57, cgokey, djohnson, Domele, Hamad, linkmx, markuschow, mistergreen2011, pungs, revg, Sloshmonster, v1n2e7t
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,655
Threads: 94,116
Posts: 402,889
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pungs
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 11:03 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0