Amazon.com Widgets Program 15.3
Welcome, Guest. Please login or register.
Did you miss your activation email?
August 01, 2014, 05:22:55 PM
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
| |-+ Program Examples
| | |-+ Chapter 15
| | | |-+ Program 15.3
Pages: [1] Go Down
Print
Author Topic: Program 15.3 (Read 3140 times)
TotalLuck
Full Member
***
Posts: 107



WWW Email




on: February 15, 2009, 01:49:30 PM

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


int main(int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc ] init];
        NSString *str1 = @"This is string A";
NSString *str2 = @"This is string B";
NSString *res;
NSComparisonResult  compareResult;

// count numbner of  characters

NSLog(@"length of str1: %lu", [str1 length]);

// copy one string to another

res = [NSString stringWithString:str1 ];
        NSLog(@"Copy: %@", res);

// copy one string to the end of another

str2 =[str1 stringByAppendingString:str2];
NSLog(@"concotenatation: %@",str2);

//test if 2 strings are equal

if ([str1 isEqualToString: res] == YES)
NSLog(@"str1 == res");
else
NSLog(@"str1 != res");

//test if one string <, == , > than another

compareResult = [str1 compare: str2];

if (compareResult == NSOrderedAscending)
NSLog(@"str1 < str2");
else if (compareResult == NSOrderedSame)
NSLog(@"str1 == str2");
else // NSrdered decending
NSLog(@"str1 > str2");

//convert a string to UPPERCASE

res = [str1 uppercaseString];
NSLog(@" Uppercase conversion: %s", [res UTF8String]);

// convert a str to lowercase

res = [str1 lowercaseString];
NSLog(@"lowercase conversion: %@", res);

NSLog(@"Original String: %@", str1);


[pool drain];

return 0;
}
[/pre]
Code: (Objective-C)
2009-02-15 12:46:24.241 Chapter15[37837:813] length of string 1: 16
2009-02-15 12:46:24.242 Chapter15[37837:813] Copy: This is string A
2009-02-15 12:46:24.244 Chapter15[37837:813] concotenatation: This is string AThis is string B
2009-02-15 12:46:24.244 Chapter15[37837:813] str1 == res
2009-02-15 12:46:24.244 Chapter15[37837:813] str1 < str2
2009-02-15 12:46:24.244 Chapter15[37837:813]  Uppercase conversion: THIS IS STRING A
2009-02-15 12:46:24.245 Chapter15[37837:813] lowercase conversion: this is string a
2009-02-15 12:46:24.245 Chapter15[37837:813] Original String: This is string A
[/pre]

Logged

Apps available on  iTunes store:
"ADACode"  iPhone and iPad versions, "ADAGuidelines" iPhone & iPad versions,  "Rehabilitation Act of 1973" for iPhone
"APokerTimer" now for iPhone http://bit.ly/h1fAJp
nosferatu
Newbie
*
Posts: 5



Email




Reply #1 on: September 02, 2010, 06:59:03 PM

Hello Stephen

I think this program has a lot of memory leak. I'm wondering whether it is done on purpose ?

First this lines here is allocating a new NSString objects

res = [NSString stringWithString:str1 ];

str2 =[str1 stringByAppendingString:str2];

This is definitely memory leak in str2 because it abandoning the original str2
which contained @"This is string B"

Then this line leak again

res = [str1 uppercaseString];

and again

res = [str1 lowercaseString];

and not even one release statement at all.

Why is that ? I thought you always preach about not to leak any memory and do not rely on the Garbage Collection service.





 
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: September 02, 2010, 08:56:37 PM

There are no memory leaks in that program.   Please refer to page 338, and then to the chapter on Memory Management (Chapter 17) for more details.

Cheers,

Steve Kochan
Logged
nosferatu
Newbie
*
Posts: 5



Email




Reply #3 on: September 06, 2010, 11:02:31 PM

Hello Stephen,

Thank's for helping me. I read your book using Kindle so when I go to position 338 it took me to a completely different place  Grin

Anyway, I just browse few pages back and found this heading title "A Quick Look at the Autorelease Pool" . In that paragraph you said "In general, you don't need to worry about releasing an object that a Foundation method returns. Sometimes the object is owned by the method that returns it. Other times, the object is newly created and added to the autorelease pool by the method."

So, please correct me if I'm wrong.

NSString is a Foundation object so we don't need to release it. The autorelease pool will look after it when we assign the pointer with something else.

But if that is so, why in the program 15.8 (Creating an Address Card) when  you override the dealloc method you release the name and email. Aren't they both NSString objects and the autorelease will release them ?

-(void) dealloc
{
 [name release];
 [email release];
 [super dealloc];
}

Or because name and email are created under the AddressCard object so the autorelease cannot handle them ?

Thank's Steve  Wink
Logged
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #4 on: September 09, 2010, 08:09:26 PM

NSString objects that are not created by alloc, copy, mutableCopy, or new are autoreleased and do not need to be released by you.  If the setter is used for a property that has either the retain or copy attribute then you will still need to release that object in dealloc.  That's because when the setter is used, the object will either be copied or retained, meaning you need to release that object yourself even if it wasn't created with one of the above-listed methods.

Cheers,

Steve
Last Edit: September 09, 2010, 08:11:29 PM by skochan Logged
beb
Newbie
*
Posts: 1


Email




Reply #5 on: December 29, 2010, 04:21:27 PM

Steve--

I have a problem with the lines in Program 15.3:

str2 = [str1 stringByAppendingString:str2];
NSLog(@"concatenation: %@",str2);

You talk about str2 being immutable but it seems in those lines to be
taking on a new value, "This is string AThis is string B", and sending it to
NSLog.

How can this be?

BEB
Logged
TotalLuck
Full Member
***
Posts: 107



WWW Email




Reply #6 on: December 29, 2010, 08:28:34 PM

BEB,

yes both str1 and str2 are immutable.  however NSString class has methods that deal with the immutable strings. 
they are called convenience methods and  you can look them up in the documentation.

when you call
Code: (Objective-C)
[someString stringByAppendingString: anotherString] 

Code: (Objective-C)
- (NSString *)stringByAppendingString:(NSString *)aString;

so in that method it takes the string i pass in and  creates a NSMutableString with str1 and then adds str2 to it, then sets a new NSString with the result and  returns that NSString as the result which is then assigned to str2.

so yes my str2 did take on a whole new  value that was created with the convenience method.

hope that makes  more sense.   Yoda i am not.

Greg
Logged

Apps available on  iTunes store:
"ADACode"  iPhone and iPad versions, "ADAGuidelines" iPhone & iPad versions,  "Rehabilitation Act of 1973" for iPhone
"APokerTimer" now for iPhone http://bit.ly/h1fAJp
Jake
Newbie
*
Posts: 14






Reply #7 on: December 29, 2010, 09:04:27 PM

What TotalLuck said is right.

Let me just add on to that. str2 is just merely a pointer. Reassigning str2 is changing where str2 is pointing to and not actually editing a immutable string.
Logged
Semaj
Newbie
*
Posts: 8






Reply #8 on: March 26, 2011, 11:38:39 AM

Why is UTF8String used to print the uppercase conversion?  Why not directly print the object, like in the lowercase example?

Uppercase Example:

NSLog(@" uppercase conversion: %s", [res UTF8String]);


Lowercase Example:


NSLog(@" res converted to Lowercase %@", res);
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.