黑马程序员技术交流社区

标题: 请教怎么解决内存泄露的问题 [打印本页]

作者: iOSyinlixian    时间: 2016-4-5 23:15
标题: 请教怎么解决内存泄露的问题
请教一个问题,FractionTest.m的代码:
#import "Fraction.h"

int main (int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     
    Fraction *aFraction = [[Fraction alloc] init];
    Fraction *bFraction = [[Fraction alloc] init];
    Fraction *resultFraction;
     
    [aFraction setTo: 1 over: 4];
    [bFraction setTo: 1 over: 2];
     
    [aFraction print];
    NSLog (@"+");
    [bFraction print];
    NSLog (@"=");
     
    resultFaction = [aFraction add: bFraction];
    [resultFraction print];
     
    [[aFraction add: bFraction] print];
    [aFraction release];
    [bFraction release];
    [resultFraction release];
     
    [pool drain];
    return 0;
}

为什么还说没有办法释放add:方法创建的Fraction类:[[aFraction add: bFraction] print];
aFraction和bFraction都是前面已经分配内存和初始化的,不知道这里所说不能释放add:方法创建的Fraction类是指哪一个?

书上还说:“问题的一个解决方案是将print方法返回它的接收者,然后可以将它释放。一个更好的解决方案是将嵌套的消息分成两个单独的消息”。
谁能描述一下书上说的两个解决方案应该如何做?特别是第一个方案,如何将print方法返回它的接收者?

谢谢!
作者: 最爱舒儿    时间: 2016-4-6 00:53
学到了                                             
作者: mengjiechen    时间: 2016-4-7 13:45
检查自定义类的setter方法重写了吗,是否使用retain,是否重写dealloc方法,是否写对




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2