NSMutableArray上的内存管理

时间:2011-03-04 01:47:25

标签: memory-leaks nsmutablearray autorelease

我读了一些关于这个的材料,然后我试着写一个简单的 应用程序但感到困惑,这里是示例(使用cocos2d模板):

MySpriteObject类:

#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface MySpriteObject : CCNode {
    CCSprite *mySprite;
    NSString *spriteInfo;
}

@property(nonatomic,retain) CCSprite *mySprite;
@property(nonatomic,retain) NSString *spriteInfo;
@end


#import "MySpriteObject.h"

@implementation MySpriteObject
@synthesize mySprite;
@synthesize spriteInfo;

-(id) init
{
    NSLog(@"MySpriteObject init");
    if ((self = [super init])) {

        self.mySprite = [CCSprite spriteWithFile:@"Icon.png"];
        self.spriteInfo = [[[NSString alloc] initWithFormat:@"sprite info"] autorelease];
    }

    return (self);
}

-(void)dealloc
{
    NSLog(@"MySpriteObject dealloc");
    [self.spriteInfo release];
    [super dealloc];
}

@end

MyObjectManager类:

#import "cocos2d.h"
#import "MySpriteObject.h"

@class MySpriteObject;
@interface MyObjectManager : CCNode {

}

+(MyObjectManager *)sharedMyObjectManager;
-(NSMutableArray *)func1;
-(NSMutableArray *)func2;

@end


#import "MyObjectManager.h"
#import "MySpriteObject.h"

@implementation MyObjectManager

static MyObjectManager *_sharedMyObjectManager = nil;

+(MyObjectManager *)sharedMyObjectManager
{
    NSLog(@"MyObjectManager sharedMyObjectManager");
    if (!_sharedMyObjectManager) {

        if( [ [MyObjectManager class] isEqual:[self class]] )
            _sharedMyObjectManager = [[MyObjectManager alloc] init];
        else
            _sharedMyObjectManager = [[self alloc] init];
    }

    return _sharedMyObjectManager;
}

-(id)init
{  
    NSLog(@"MyObjectManager init");
    if( (self = [super init]) ) {
    }
    return self;
}

-(void)dealloc
{
    NSLog(@"MyObjectManager dealloc");
    [super dealloc];
}

-(NSMutableArray *)func1
{
    NSMutableArray *array1 = [[NSMutableArray alloc] init];
    array1 = [self func2];
    return array1;
}

-(NSMutableArray *)func2;
{
    NSMutableArray *array2 = [[NSMutableArray alloc] init];
    for (int i = 0; i < 3; i++) {

        MySpriteObject *mySpriteObject = [[MySpriteObject alloc] init];
        [array2 addObject:mySpriteObject];

        //[mySpriteObject release];
    }
    return array2;
}
@end

在“HelloWorldScene.m”初始化方法中:

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

        NSMutableArray *array = [[NSMutableArray alloc] init];
        array = [[MyObjectManager sharedMyObjectManager] func1];
        for (int i = 0; i < array.count; i++) { 
            MySpriteObject *s = [[MySpriteObject alloc] init];
            s = [array objectAtIndex:i];
            NSLog(@"%@", s.spriteInfo);
            [s release];
        }
        [array release];
    }
    return self;
}

上面发布的代码工作正常(至少没有崩溃),但我认为有 记忆泄漏了。

首先,我有fun1 fun2方法返回这样的数组:

return [array1 autorelease];
return [array2 autorelease];

然后在func2中释放了SpriteObject,因为你可以看到注释行。

但应用程序崩溃了。

然后我取消注释SpriteObject释放线,但它仍然崩溃。

然后我在return语句中删除了两个autorelease并且它有效 细

根据上面的代码,有人可以给出一些建议吗? 提前谢谢。

1 个答案:

答案 0 :(得分:1)

哦,男孩,有多个问题。首先要记住释放你所谓的“分配,新,保留,复制”的所有内容。请参阅Objective C release, autorelease, and data typesRelease, Dealloc, and the Self reference

这里的基本问题是,您要覆盖以前创建的变量。例如:

  

NSMutableArray * array = [[NSMutableArray alloc] init];

     

array = ...;

     

[阵列发布];

因此,您会创建内存泄漏并获得双重释放问题。有关如何正确执行此操作,请参阅obj-c NSString and alloc / retain / release