Welcome, Guest. Please login or register.
Did you miss your activation email?
September 09, 2010, 09:26:47 AM
Home Help Search chat Login Register   
News:   If you like the book and forum please  write a review on Amazon   Recorded lessons available for iPad viewing and download....in process! Consulting and onsite training available!   If you're running Xcode 3.2 (or later), see this post   Follow me on Twitter  @skochan  Forum Guests: register to download (or see) attachments.    Join our mailing list.


+  Official Forum for Programming in Objective-C 2.0 (the iPhone Programming Language) - Stephen Kochan
|-+  Programming in Objective-C 2.0, 2nd ed. (Stephen Kochan)
| |-+  Answers to Exercises
| | |-+  Chapter 15
| | | |-+  Exercise 15-3 (is this the proper way to go about avoiding a memory leak?)
« previous next »
Pages: [1] Go Down Print
Author Topic: Exercise 15-3 (is this the proper way to go about avoiding a memory leak?)  (Read 267 times)
mdziedzic
Newbie
*
Posts: 41



WWW
« on: December 08, 2009, 11:13:45 AM »

From AddressBook.h:

Code: (Objective-C)
-(NSMutableArray *) lookup: (NSString *) theName
{
NSMutableArray *matches = [[NSMutableArray alloc] init];

for (AddressCard *nextCard in book) {
NSRange textSearch = [[nextCard name] rangeOfString: theName options: NSCaseInsensitiveSearch];
if (textSearch.location != NSNotFound)
[matches addObject: nextCard];
}

[matches autorelease];
return matches;
}

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


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

NSString *aName = @"Julia Kochan";
NSString *aEmail = @"jewls377@axlc.com";
NSString *bName = @"Tony Iannino";
NSString *bEmail = @"tony.iannino@techfitness.com";
NSString *cName = @"Stephen Kochan";
NSString *cEmail = @"steven@kochan.wood.com";
NSString *dName = @"Jamie Baker";
NSString *dEmail = @"jbaker@kochan-wood.com";
NSString *eName = @"Maki Dziedzic";
NSString *eEmail = @"maki@eggfoo.com";


AddressCard *card1 = [[AddressCard alloc] init];
AddressCard *card2 = [[AddressCard alloc] init];
AddressCard *card3 = [[AddressCard alloc] init];
AddressCard *card4 = [[AddressCard alloc] init];
AddressCard *card5 = [[AddressCard alloc] init];


AddressBook *myBook = [AddressBook alloc];
NSMutableArray *matchedCards;

// first set up four address cards

[card1 setName: aName andEmail: aEmail];
[card2 setName: bName andEmail: bEmail];
[card3 setName: cName andEmail: cEmail];
[card4 setName: dName andEmail: dEmail];
[card5 setName: eName andEmail: eEmail];


myBook = [myBook initWithName: @"Michael's Address Book"];

// add some cards to the address book

[myBook addCard: card1];
[myBook addCard: card2];
[myBook addCard: card3];
[myBook addCard: card4];
[myBook addCard: card5];

// look up a person by name

NSString *searchTerm = @"ko";

NSLog(@"Lookup: %@", searchTerm);
matchedCards = [myBook lookup: searchTerm];

if (matchedCards != nil) {
for (AddressCard *theCard in matchedCards)
[theCard print];
} else {
NSLog(@"Not found!");
}

[card1 release];
[card2 release];
[card3 release];
[card4 release];
[card5 release];
[myBook release];

[pool drain];
    return 0;
}

Logged
mdziedzic
Newbie
*
Posts: 41



WWW
« Reply #1 on: December 08, 2009, 11:22:46 AM »

Or better...

Code: (Objective-C)
-(NSMutableArray *) lookup: (NSString *) theName
{
NSMutableArray *matches = [[[NSMutableArray alloc] init] autorelease];

for (AddressCard *nextCard in book) {
NSRange textSearch = [[nextCard name] rangeOfString: theName options: NSCaseInsensitiveSearch];
if (textSearch.location != NSNotFound)
[matches addObject: nextCard];
}

return matches;
}
Logged
skochan
Administrator
Hero Member
*****
Posts: 2250



« Reply #2 on: December 08, 2009, 12:24:38 PM »

Yep, they're both fine!

Cheers,

Steve Kochan
Logged
kadegray
Newbie
*
Posts: 12


Email
« Reply #3 on: February 04, 2010, 04:44:37 PM »

Im doing this exercise now. So im yet to do chapter 17 on Memory Managment.

If you just used autorelease, then you used that method a whole bunch of times, objects will keep being made and added to the autorelease pool, hence wasting memory while the program is running. Is this true?

So I'm trying to remember how to release the object the method created after its been returned. I'm sure there was a tidier way of doing it, rather than just going [object release] after your done with the object the method created. Or is that all you do?
Logged
skochan
Administrator
Hero Member
*****
Posts: 2250



« Reply #4 on: February 08, 2010, 10:47:47 AM »

If you just used autorelease, then you used that method a whole bunch of times, objects will keep being made and added to the autorelease pool, hence wasting memory while the program is running. Is this true?

Yes, that's true

Quote
So I'm trying to remember how to release the object the method created after its been returned. I'm sure there was a tidier way of doing it, rather than just going [object release] after your done with the object the method created. Or is that all you do?

Yeah, there's no other trick for handling that.

Cheers,

Steve
Logged
Pages: [1] Go Up Print 
« previous next »
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.