NSArray内存管理(NSArray Memory Management)

编程入门 行业动态 更新时间:2024-10-28 12:28:49
NSArray内存管理(NSArray Memory Management)

出于某种原因,当我发布NSArray时,我得到了EXC_BAD_ACCESS异常。 这是实施:

-(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很快就变得无关,当pathsForResourcesOfType返回一个autorelease对象时。 这意味着我不应该手动释放imagesPath对象。

应使用以下行:

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

更新2:

与这篇文章有关的另一个问题。 在下面的代码中,我手动初始化一个新的NSMutableArray。

alphabets = [[NSMutableArray alloc] init];

后来我将CCSprite(Cocos2d对象)插入到字母数组中。 CCSprite是自动释放对象。 我还需要手动发布字母吗? 因为,经过一段时间后所有对象都被释放并且内存将被返回,但是那么在字母表NSMutable数组中会留下什么?

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;

}

UPDATE 1:

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.

The following line should be used:

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

UPDATE 2:

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

alphabets = [[NSMutableArray alloc] init];

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的方法获取对象时才应该释放对象(标准方法遵循该规则,您也应该坚持使用它)。

在您的情况下,您使用pathsForResourcesOfType:方法获取imagesPath对象, pathsForResourcesOfType:方法返回一个自动释放的对象,因此您pathsForResourcesOfType:释放它。

编辑:是的,您需要在某处释放alphabets对象(出于同样的原因 - 您使用alloc方法获得它)。

Objective-c容器获取添加到它们的对象的所有权,当对象被添加到数组时它们被保留,因此可以保证它们的生命周期至少与容器的生命周期一样长。 当您从集合中移除对象或集合本身被销毁时,其成员将被释放(以补偿在添加时保留)。

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).

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

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

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).

更多推荐

本文发布于:2023-08-05 20:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1437732.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:内存管理   NSArray   Management   Memory

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!