开发者

getting leak trying to release NSMutableArray

I'm getting memory leak releasing NSMutableArray in a UIViewController that spins up, then in ViewDidLoad it allocs and inits the array, adds objects to it; and then when view closes: its dealloc() releases each array object, then releases the array.

And a leak usually results.

My basic structure: ...

...m  file:

NSMutableArray* foo;

@implementat开发者_运维技巧ion ....

viewDidLoad
{
[[foo  alloc]  init];
...
}

dealloc
{
  for i = each foo object:
    [foo  objectAtIndex: i]  release];

    [foo  release];
}

...


The leak in this case can result when the items in your array are being retained elsewhere. Sending a release message to that item will just decrease its retain count and will not actually dealloc it.


When releasing a NSMutableArray, it handles releasing all it's children. Same goes for NSArray, NSMutableDictionary, NSDictionary, etc etc.

Try setting up foo as an instance variable in your header and then synthesize it:

...h file
@interface MyObject : NSObject {
    NSMutableArray* foo;
}

@property (nonatomic, retain) NSMutableArray *foo;

...m file
@implementation ....
@synthesize foo;

viewDidLoad
{
    self.foo = [[NSMutableArray  alloc]  init];
    ...
}

dealloc
{
    [foo  release];
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜