Doug Null
Doug Null

Reputation: 8327

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;

@implementation ....

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

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

    [foo  release];
}

...

Upvotes: 0

Views: 424

Answers (2)

coryjacobsen
coryjacobsen

Reputation: 1018

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];
}

Upvotes: 1

Mridul Kashatria
Mridul Kashatria

Reputation: 4187

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.

Upvotes: 1

Related Questions