ARC下NSException有可能会引起内存泄露

在使用Instrumnents对程序进行内存测试的时候,发现有几处异常处理的地方提示内存泄露。经过分析得知,因为异常处理,改变代码执行路径,导致编译生成的 release 代码没有执行

简单写了些测试代码验证:

- (void)viewDidLoad {

    [super viewDidLoad];

    @try {
        NSMutableArray *stringList = [[NSMutableArray alloc] init];
        for (int i = 0; i < 100; i++) {
            [stringList addObject: @"string instance."];
        }
        [self throwException];
    }
    @catch (NSException *exception) {
        NSLog(@"catch Exception : %@", exception);
    }
    @finally {

    }
}

- (void)throwException
{
    [NSException raise:@"An Exception." format:@"Exception Description."];
}

反复进出这个页面几次后,在Instruments上果然看到提示内存泄露,而且泄露的地方就指明是 @try{ } 里面创建的对象。

img)

解决思路

1, 尽量不要在@try{}里面有操作,导致对象的引用引用计数增加。@try{ } 语句块里面只有有可能抛出异常的语句。

修改后的代码:

- (void)viewDidLoad {

    [super viewDidLoad];

    NSMutableArray *stringList = [[NSMutableArray alloc] init];
    for (int i = 0; i < 100; i++) {
        [stringList addObject: @"string instance."];
    }

    @try {
        [self throwException];
    }
    @catch (NSException *exception) {
        NSLog(@"catch Exception : %@", exception);
    }
    @finally {

    }
}

最后用Instruments检查一下,一切正常了。

2, 查看了苹果的开发文档,发现有这么一段描述:

>
By default in Objective C, ARC is not exception-safe for normal releases:
It does not end the lifetime of __strong variables when their scopes are abnormally terminated by an exception.
It does not perform releases which would occur at the end of a full-expression if that full-expression throws an exception.
A program may be compiled with the option -fobjc-arc-exceptions in order to enable these, or with the option -fno-objc-arc-exceptions to explicitly disable them, with the last such argument “winning”.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值