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 retain
ed 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];
}
精彩评论