此Objective-C嵌套NSArray会在iPhone上引起内存泄漏吗? [英] Will this Objective-C, nested NSArray cause a memory leak on iPhone?

查看:79
本文介绍了此Objective-C嵌套NSArray会在iPhone上引起内存泄漏吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

NSArray *tLines = [[NSArray alloc] initWithObjects:
                   [[NSArray alloc] initWithObjects:@"Matt", @"David", nil],
                   [[NSArray alloc] initWithObjects:@"Bob", @"Ken", nil], nil];
self.lines = tLines;
[tLines release];

我正在alloc中的alloc中,当我调用[lines release];时,嵌套数组会被释放吗?

I'm allocing NSArrays within an NSArray, will the nested arrays get released when I call [lines release];?

推荐答案

否,您不应该这样做. alloc阵列时,需要释放它们.包含数组将处理其对象的内存管理(添加对象时保留它们,删除对象时释放它们),但这还不包括为数组创建的对象正确释放它们的职责.您应该使用便捷的构造函数来创建数组,或者一次创建一个数组,然后在将它们添加到主数组后将其释放.

No, you should not do this. When you alloc the arrays, you need to release them. The containing array will handle memory management for its objects (retaining them when they're added, releasing when they're removed), but that is beside the responsibility of the object that created to the arrays to properly release them. You should either use a convenience constructor to create the arrays or else create them one at a time and release them after they've been added to the master array.

这篇关于此Objective-C嵌套NSArray会在iPhone上引起内存泄漏吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆