leak in @property setter
I have a custom UIView that I add as a subview in severa开发者_运维问答l places throughout my app. I send the view a NSMutable Array by setting a property and it displays a graph of notes. This works fine except for one view in my app. I use this code in the viewDidLoad section of each view that contains the graph.
endNoteDisplay =[[NoteDisplay alloc] initWithFrame:CGRectMake(0,0,320,180)];
endNoteDisplay.tag = 100;
endNoteDisplay.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Note_Bkg.png"]];
NSMutableArray *tmpNts = [[NSMutableArray alloc] initWithObjects: @"C", @"E", @"G", @"A",nil];
endNoteDisplay.noteSpread = tmpNts;
[tmpNts release];
[self.view addSubview:endNoteDisplay];
[endNoteDisplay release];
If I remove the
endNoteDisplay.noteSpread = tmpNts;
line everything works just fine no leak.If I leave it in Instruments is showing a memory leak. The leak occurs when I leave the current view (where I display the notes) and return to the previous view (it has a table with a list of different note options to be displayed).
- My app is working as expected and this is the only leak that is coming up.
Can anybody tell me why this is generating a leak? the noteSpread
property in my NoteDisplay
is set to nonatomic retain
.
Does the -dealloc
method in NoteDisplay release the noteSpread property?
In NoteDisplay's dealloc you need to send release to the instance variable that backs noteSpread.
you can use this combine statement:
endNoteDisplay.noteSpread = [NSMutableArray arrayWithObjects: @"C", @"E", @"G", @"A",nil];
which will eliminate these 2 lines,
NSMutableArray *tmpNts = [[NSMutableArray alloc] initWithObjects: @"C", @"E", @"G", @"A",nil];
endNoteDisplay.noteSpread = tmpNts;
and will also remove the leak:
just be sure to release the "noteSpread" in the controller when you're done with your work.
精彩评论