NSArray内存管理 [英] NSArray Memory Management

查看:40
本文介绍了NSArray内存管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某些原因,当我释放NSArray时,会得到EXC_BAD_ACCESS异常.这是实现:

For some reason when I release the NSArray I get the EXC_BAD_ACCESS exception. Here is the implementation:

-(void) loadAllAlphabets
{
    NSBundle *bundle = [NSBundle mainBundle]; 
    NSArray *imagesPath = [[NSArray alloc] init]; 

    imagesPath = [bundle pathsForResourcesOfType:@"png" inDirectory:@"Images"];

    alphabets = [[NSMutableArray alloc] init]; 

    NSString *fileName = [[NSString alloc] init]; 

    for(int i=0; i<= imagesPath.count -1 ; i++) 
    {
        fileName = [[imagesPath objectAtIndex:i] lastPathComponent];
        CCSprite *sprite = [CCSprite spriteWithFile:fileName];

        sprite.userData = [[fileName stringByDeletingPathExtension] uppercaseString];   

        [alphabets addObject:sprite]; 
    }

    // release fileName 
    [fileName release]; 
    fileName = nil; 

    [imagesPath release]; // this causes the application to crash with EXC_BAD_ACCESS
//  imagesPath = nil; 

}

更新1:

因此,问题是,尽管我负责释放imagesPath对象,因为我使用了alloc,但当pathForResourcesOfType返回一个自动释放对象时,它很快就变得无关紧要了.这意味着我不应该手动释放imagesPath对象.

So, the problem was that although I was responsible for releasing the imagesPath object since I used alloc that soon become irrelevant when pathsForResourcesOfType returned an autorelease object. This means I should not release the imagesPath object manually.

应使用以下行:

NSArray *imagesPath = [bundle pathsForResourcesOfType:@"png" inDirectory:@"Images"];

更新2:

与此帖子相关的另一个问题.在下面的代码中,我手动初始化了一个新的NSMutableArray.

Another question which is related to this post. In the following code I initialize a new NSMutableArray manually.

alphabets = [[NSMutableArray alloc] init]; 

稍后,我将CCSprite(Cocos2d对象)插入字母数组.CCSprite是自动释放对象.我还需要手动释放字母吗?因为,在一段时间之后,所有对象都被释放并且内存将被返回,但是字母NSMutable数组中将剩下什么呢?

Later I insert CCSprite (Cocos2d objects) into alphabets array. CCSprite are autorelease objects. Do I still have to release alphabets manually? Since, after some time all objects are released and memory will be returned but then what will be left inside alphabets NSMutable array?

推荐答案

内存管理中的一般经验法则-仅当使用包含new,copy或alloc的方法获得对象时,才应释放该对象(遵循标准方法)该规则,您也应该遵守该规则).

general rule of thumb in memory management - you should release an object only if you obtain it using method that contains new, copy or alloc in it (standard method follow that rule and you should stick to it as well).

在您的情况下,您可以使用 pathsForResourcesOfType:方法获取imagesPath对象,该方法返回一个自动释放的对象,因此您不必自己释放它.

In your case you obtain imagesPath object using pathsForResourcesOfType: method which returns an autoreleased object so you must not release it yourself.

是的,您需要在某个地方释放 alphabets 对象(出于相同的原因-您通过alloc方法获得了它).

yes, you need to release alphabets object somewhere (for the same reason - you got it with alloc method).

objective-c容器拥有添加到其中的对象的所有权,这样,当我们将对象添加到数组中时,它们将得到保留,因此可以确保它们的生存时间至少与容器的生存时间一样长.当您从集合中删除对象或集合本身被销毁时,其成员将被释放(以补偿添加时的保留).

Objective-c containers take an ownership of objects added to them, that us when objects are added to an array they get retained so it is guaranteed that their life time is at least as long as the life time of container. When you remove object from collection or collection itself is destroyed then its members get released (to compensate retain on add).

这篇关于NSArray内存管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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