如何管理NSMutableArray的NSMutableArray的内存/使用情况

时间:2021-10-24 16:52:15

I am currently trying to write a class to create faux grid system to keep track of a NSMutableArray of game entities using a NSMutableArray of NSMutableArrays. Given my limited experience with Objective-C programming, I am unsure of how certain things work.

我目前正在尝试编写一个类来创建虚假网格系统,以使用NSMutableArray的NSMutableArrays跟踪游戏实体的NSMutableArray。鉴于我对Objective-C编程的经验有限,我不确定某些事情是如何工作的。

Here is the init method:

这是init方法:

#define MAX_BALL_ROWCOUNT 6
#define MAX_BALL_COLCOUNT 4
- (id) initWithMutableArray:(NSMutableArray *)aList {
self = [super init];
if (self != nil) {
    ballList = [[NSMutableArray alloc] initWithCapacity: MAX_BALL_ROWCOUNT];

    for (int i=0; i<MAX_BALL_ROWCOUNT; i++) {
        NSMutableArray *balls = [[NSMutableArray alloc] initWithCapacity:MAX_BALL_COLCOUNT];
        [ballList addObject:balls];
        [balls release];
    }

    int x = 0;

    for (NSMutableArray *array in ballList) {
        for (int i = 0; i<MAX_BALL_COLCOUNT; i++) {
            [array addObject:[aList objectAtIndex:x]];
            x++;
            }
        }
    }

return self;
}

ballList is the class's NSMutableArray that will store NSMutableArrays.

ballList是类的NSMutableArray,它将存储NSMutableArrays。

aList is the NSMutableArray containing the GameEntities I wish to keep track of that is passed into this class.

aList是NSMutableArray,包含我希望跟踪传递到此类的GameEntities。

All the sizes and amount of entities to store are fixed, which is why there is no checks on the sizes of the arrays nor the number of entities to store.

要存储的实体的所有大小和数量都是固定的,这就是为什么不检查数组的大小以及要存储的实体的数量。

So the first question I have involves freeing memory. This is the dealloc function I currently have:

所以我的第一个问题涉及释放记忆。这是我目前拥有的dealloc功能:

- (void) dealloc {
    [ballList release];
    [super dealloc];
}

Does calling a release on ballList cause the release to be called on the NSMutableArrays that it contains (which will subsequently call the release on the objects those NSMutableArrays contain) or do I have to write something like:

调用ballList上的发布会导致在它包含的NSMutableArrays上调用release(随后会调用NSMutableArrays包含的对象上的发行版),或者我必须编写如下内容:

for (NSMutableArray *array in ballList) {
    [array release];
}
[ballList release];

My second question involves the usage of this array of arrays. Is this the proper way to traverse through ballList?

我的第二个问题涉及使用这个数组数组。这是遍历ballList的正确方法吗?

- (void) update {
    for (NSMutableArray *array in ballList) {
        for (GameEntity *balls in array) {
            (CGPoint) location = [balls getLocation];
            [balls setLocation: CGPointMake(location.x+1, location.y+1)];
        }
    }
}

Lastly, in the code above where it sets the balls location, does it only affect the contents within ballList or does the original aList that is passed into ballList change as well? If the contents in the original aList do not change, how would I write it so that they do?

最后,在上面设置球位置的代码中,它是否只影响ballList中的内容,或者传递给ballList的原始aList是否也会发生变化?如果原始aList中的内容没有改变,我该如何编写它们呢?

If people have suggestions for a better way to keep track of the entities in a grid system, I'd be open to those too. Thanks in advance.

如果有人建议更好地跟踪网格系统中的实体,我也会对这些实体持开放态度。提前致谢。

2 个解决方案

#1


0  

  1. When the dealloc of a NSArray or NSMutableArray is called, all its contents gets a release message. So when you release ballList, if there there is no other owner (I guess in this case there is none) then its dealloc is called and you don't need to release the other arrays here.

    当调用NSArray或NSMutableArray的dealloc时,其所有内容都会获得一条释放消息。所以当你释放ballList时,如果没有其他所有者(我想在这种情况下没有),那么它的dealloc被调用,你不需要在这里释放其他数组。

  2. Your loop traversal is fine. Though for 2D arrays instead of NSArray of NSArray I personally prefer pure C 2D array, at least in most of the cases.

    你的循环遍历很好。虽然对于2D阵列而不是NSArray的NSArray,我个人更喜欢纯C 2D阵列,至少在大多数情况下。

  3. When you are adding object in this way you are adding a reference in the array. So any change via the array's reference will be reflected in all references of the object. If you don't want that then add a copy of the object in the array.

    以这种方式添加对象时,您将在数组中添加引用。因此,通过数组引用的任何更改都将反映在对象的所有引用中。如果您不想这样,那么在数组中添加该对象的副本。

#2


1  

First : One release is enough for the NSMutableArray instance to release all it's object.

第一:一个版本足以让NSMutableArray实例释放它的所有对象。

    [ballList release];

Second : Your code for updating GameEntity instance is fine and will also effect to the original aList (which you called) .

第二:你更新GameEntity实例的代码很好,也会影响原来的aList(你调用的)。

#1


0  

  1. When the dealloc of a NSArray or NSMutableArray is called, all its contents gets a release message. So when you release ballList, if there there is no other owner (I guess in this case there is none) then its dealloc is called and you don't need to release the other arrays here.

    当调用NSArray或NSMutableArray的dealloc时,其所有内容都会获得一条释放消息。所以当你释放ballList时,如果没有其他所有者(我想在这种情况下没有),那么它的dealloc被调用,你不需要在这里释放其他数组。

  2. Your loop traversal is fine. Though for 2D arrays instead of NSArray of NSArray I personally prefer pure C 2D array, at least in most of the cases.

    你的循环遍历很好。虽然对于2D阵列而不是NSArray的NSArray,我个人更喜欢纯C 2D阵列,至少在大多数情况下。

  3. When you are adding object in this way you are adding a reference in the array. So any change via the array's reference will be reflected in all references of the object. If you don't want that then add a copy of the object in the array.

    以这种方式添加对象时,您将在数组中添加引用。因此,通过数组引用的任何更改都将反映在对象的所有引用中。如果您不想这样,那么在数组中添加该对象的副本。

#2


1  

First : One release is enough for the NSMutableArray instance to release all it's object.

第一:一个版本足以让NSMutableArray实例释放它的所有对象。

    [ballList release];

Second : Your code for updating GameEntity instance is fine and will also effect to the original aList (which you called) .

第二:你更新GameEntity实例的代码很好,也会影响原来的aList(你调用的)。