开发者

NSMutableArray memory woes

I have a result class that has a NSMutableArray within it that is causing开发者_C百科 me memory leaks. I have been crawling Google to try and find out why but nothing is working.

Can anyone see a memory leak here?

@interface Response : NSObject 
{
    NSMutableArray *Items;
}
@property (nonatomic, retain) NSMutableArray *Items;

----

-(id)init {
    self = [super init];
    if (!self) {
        return nil;
    }
    self.Items = [[NSMutableArray alloc] init];
    return self;
}
-(void)dealloc
{
    [self.Items release], self.Items = nil;
    [Items release], Items = nil;
    [super dealloc];
}

Then its used like this:

-(void)Update
{
    Response *resp = [self getResponse];
    foreach(GameObject *o in resp.items){
        //Do Stuff
    }
}
-(Response*)getResponse
{
    Response *result = [[Response alloc] init];
    //Loop through things
    [result.items addObject:o];
    //Finish looping stuff
    return result;
}

Im stuck at trying to get this memory leak gone. Any help is greatly appreciated.


Thew following line is the issue...

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

Your items property has a 'retain' attribute which ups the retain count but you also have a retain count of 1 from the alloc. So instead do either of these....

self.items = [[[NSMutableArray alloc] init] autorelease];

or

NSMutableArray *newArray = [[NSMutableArray alloc] init];
self.items = newArray;
[newArray release];


Pretty sure that your Response object is what's leaking, as you alloc/init and return it, and dealloc will never be called. You should mark it as autorelease in getResponse()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜