Objective-C:在另一个对象的init中分配对象(内存管理)

时间:2022-09-07 11:20:48

In my .h file I have:

在我的.h文件中,我有:

NSMutableArray *myArray;
@property (nonatomic, retain) NSMutableArray *myArray;

My .m file looks basically like this:

我的.m文件看起来基本上是这样的:

@synthesize myArray;

- (id) init {
    self = [super init];

    if (self != nil)
    {
        self.myArray = .... ? // here I want to create an empty array
    }

    return self;
}

- (void) dealloc {
    [self.myArray release];

    [super dealloc];
}

What I'm not sure about is what do to in the init.

我不确定的是在init中做了什么。

1)

self.myArray = [[NSMutableArray alloc] init];

2)

NSMutableArray *tmp = [[NSMutableArray alloc] init];
self.myArray = tmp;
[tmp release];

Solution 1 doesn't seem right to me, because of my @property (retain) setting I automatically increase the retain counter when setting self.myArray, but additionally I have already a "+1 retain" due to the [NSMutableArray alloc] and then ending up with a retain count of 2 for that object, but only releasing once in the dealloc. Thus the second solution seems more correct to me, even though it is cumbersome.

解决方案1对我来说似乎不对,因为我的@property(保留)设置我在设置self.myArray时会自动增加保留计数器,但另外由于[NSMutableArray alloc]而我已经有一个“+1保留”然后以该对象的保留计数为2结束,但只在dealloc中释放一次。因此第二种解决方案对我来说似乎更正确,即使它很麻烦。

Also am I wondering if self.myArray = ... is actually the same as [self setMyArray:...] and thus does increase the retain count.

我也想知道self.myArray = ...实际上是否与[self setMyArray:...]相同,因此确实增加了保留计数。

UPDATE

I actually found the answers (and even more details) here in case anyone is interested in reading more.

我实际上在这里找到答案(甚至更多细节)以防任何人有兴趣阅读更多。

3 个解决方案

#1


4  

self.myArray = is exactly the same as [self setMyArray:...].

self.myArray =与[self setMyArray:...]完全相同。

You could however do myArray = [[NSMutableArray alloc] init]; which would end up with a retain count of 1 and would be totally legit.

但是你可以做myArray = [[NSMutableArray alloc] init];最终保留计数为1并且完全合法。

#2


1  

Yes, self.myArray = ... is the same as [self setMyArray:...]

是的,self.myArray = ...与[self setMyArray:...]相同

So the set process adds a redundant retain to your object. You should probably release it immediately after the set, although I have seen some code that uses autorelease, one way or another. Both approaches are awkward.

因此,设置过程会为您的对象添加冗余保留。您可能应该在集合之后立即释放它,尽管我已经看到一些使用autorelease的代码,无论如何。这两种方法都很尴尬。

Avoiding the set accessor (myArray = ...) is possible, but bypassing accessors is also frowned on by purists.

避免设置访问器(myArray = ...)是可能的,但绕过访问器也是纯粹主义者不赞同的。

#3


1  

Another option is to use the convenience methods returning autoreleased instances:

另一种选择是使用返回自动释放实例的便捷方法:

self.myArray = [NSMutableArray array];

#1


4  

self.myArray = is exactly the same as [self setMyArray:...].

self.myArray =与[self setMyArray:...]完全相同。

You could however do myArray = [[NSMutableArray alloc] init]; which would end up with a retain count of 1 and would be totally legit.

但是你可以做myArray = [[NSMutableArray alloc] init];最终保留计数为1并且完全合法。

#2


1  

Yes, self.myArray = ... is the same as [self setMyArray:...]

是的,self.myArray = ...与[self setMyArray:...]相同

So the set process adds a redundant retain to your object. You should probably release it immediately after the set, although I have seen some code that uses autorelease, one way or another. Both approaches are awkward.

因此,设置过程会为您的对象添加冗余保留。您可能应该在集合之后立即释放它,尽管我已经看到一些使用autorelease的代码,无论如何。这两种方法都很尴尬。

Avoiding the set accessor (myArray = ...) is possible, but bypassing accessors is also frowned on by purists.

避免设置访问器(myArray = ...)是可能的,但绕过访问器也是纯粹主义者不赞同的。

#3


1  

Another option is to use the convenience methods returning autoreleased instances:

另一种选择是使用返回自动释放实例的便捷方法:

self.myArray = [NSMutableArray array];