Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
January 23, 2018, 09:04:40 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: exercise 15-8 memory question  (Read 1377 times)
mdziedzic
Newbie
*
Posts: 41



WWW
« on: December 09, 2009, 01:53:55 PM »

In main below, do I need to release my NSMutableArray "fractions"? As I did not myself do the alloc?


Added to Fraction.m
Code: (Objective-C)
-(NSComparisonResult) compareFractions: (id) element
{
if ([self convertToNum] < [element convertToNum])
return NSOrderedAscending;
else if ([self convertToNum] > [element convertToNum])
return NSOrderedDescending;
else
return NSOrderedSame;
}


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


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

Fraction *frac1 = [[Fraction alloc] init];
Fraction *frac2 = [[Fraction alloc] init];
Fraction *frac3 = [[Fraction alloc] init];
Fraction *frac4 = [[Fraction alloc] init];
Fraction *frac5 = [[Fraction alloc] init];

    [frac1 setTo: 5 over: 7];
[frac2 setTo: 3 over: 7];
[frac3 setTo: 1 over: 9];
[frac4 setTo: 3 over: 4];
[frac5 setTo: 1 over: 4];

NSMutableArray *fractions = [NSMutableArray arrayWithObjects:
frac1, frac2, frac3, frac4, frac5, nil];

NSLog(@"Unordered: ");

for (Fraction *theFrac in fractions)
[theFrac print];

[fractions sortUsingSelector: @selector(compareFractions:)];

NSLog(@"Ordered: ");

for (Fraction *theFrac in fractions)
[theFrac print];

[frac1 release];
[frac2 release];
[frac3 release];
[frac4 release];
[frac5 release];

[pool drain];
    return 0;
}


Unordered:
5/7
3/7
1/9
3/4
1/4
Ordered:
1/9
1/4
3/7
5/7
3/4
« Last Edit: December 09, 2009, 01:57:55 PM by scarbelly » Logged
skochan
Administrator
Hero Member
*****
Posts: 3114



« Reply #1 on: December 09, 2009, 01:57:39 PM »

No, it was added to the autorelease pool automatically.  As  you'll see in Chapter 17,  you only are responsible for releasing objects created with alloc, copy (or mutableCopy), or new.  You can assume objects created by any other Foundation methods have been autoreleased.

Cheers,

Steve
Logged
mdziedzic
Newbie
*
Posts: 41



WWW
« Reply #2 on: December 09, 2009, 02:02:13 PM »

Thanks for the quick reply!

That really helps. That's what I was beginning to suspect, but it really helps to have you say so.

Slowly it is beginning to gel.

& thanks for the great book and the online help. It is a wonderful way to learn the material.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.