#import "Fraction.h"int main( int argc,const char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Fraction * aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init],*sum2;
int i , n, pow2;
[sum setTo: 0 over:1];
NSLog(@"En ter your value for n:");
scanf("%i",&n);
pow2 = 2;
for(i = 1; i <= n; ++i)
{
[aFraction setTo:1 over:pow2];
sum2 = [sum add:aFraction];
[sum release];
sum = sum2;
pow2 *= 2;
}
NSLog(@"After %i iterations,the sum is %g", n, [sum convertToNum]);
[aFraction release];
[sum release];
[pool drain];
return 0;
}这样运行正常,但是我不明白,为什么sum2不用release.在后边加个release 就错了〜
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Fraction * aFraction = [[Fraction alloc] init];
Fraction *sum = [[Fraction alloc] init],*sum2;
int i , n, pow2;
[sum setTo: 0 over:1];
NSLog(@"En ter your value for n:");
scanf("%i",&n);
pow2 = 2;
for(i = 1; i <= n; ++i)
{
[aFraction setTo:1 over:pow2];
sum2 = [sum add:aFraction];
[sum release];
sum = sum2;
pow2 *= 2;
}
NSLog(@"After %i iterations,the sum is %g", n, [sum convertToNum]);
[aFraction release];
[sum release];
[pool drain];
return 0;
}这样运行正常,但是我不明白,为什么sum2不用release.在后边加个release 就错了〜
其中[sum add:aFraction] 给sum发送一个add的消息,而这个add函数内部会调用autorelease,由NSAutoreleasePool 来管理,并不立即释放,而是当你使用完毕之后,在程序的当前这次消息循环结束时真正释放。