Objective c, Memory management of instance members

后端 未结 6 774
南方客
南方客 2020-12-02 00:58

I am confuzed by the memory management of instance members. I have a class with a ivar:

DetailedResultsTableViewController *detailedResultsTableViewControlle         


        
6条回答
  •  时光取名叫无心
    2020-12-02 01:32

    First you should not look at the retaincount, it not really reliable.

    Second your property is set to retain. So when you assign some thing to it, it will increase the reatincount. As will alloc.

    Doing it like this you are leaking:

    self.detailedResultsMapViewController = [[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil];
    

    you should do:

    DetailedResultsMapViewController *vc = [[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil];
    self.detailedResultsMapViewController =vc;
    [vc release], vc= nil;
    

    Or use Autorelease:

    self.detailedResultsMapViewController = [[[DetailedResultsMapViewController alloc] initWithNibName:@"DetailedResultsMapViewController" bundle:nil] autorelease];
    

提交回复
热议问题