Where is the memory leak in my UITableViewController?

后端 未结 5 643
逝去的感伤
逝去的感伤 2021-01-27 18:00

The table view works fine however when I leave the view and come back to it the second time, I get a memory leak. Probably something in the viewDidLoad just not sure.

I

相关标签:
5条回答
  • 2021-01-27 18:14

    You have several places where you call [[XXX alloc] init] where XXX is a NSMutableArray or NSMutableString. You never release these and they are not autoreleased, thus they leak.

    0 讨论(0)
  • 2021-01-27 18:15

    I heard the

    [UIImage imageNamed:@"contact.png"];

    is a memory occurring line. It will create an autorelease object as the returned object. May be that can also be a problem.

    0 讨论(0)
  • 2021-01-27 18:33

    In tableView:didSelectRowAtIndexPath:, lastIndexPath is not released before it retains indexPath. In viewDidLoad, you nil out the variables without releasing them.

    And in XML parsing, you create and release strings based on the assumption that one element doesn't contain the other. If they are nested, one element will start before the other ends. In such case, you lose the reference to the original string and thus leak. This is not much of an issue if they are siblings but by the looks of you log, the elements are nested.

    0 讨论(0)
  • 2021-01-27 18:38

    I see most of your statement are ready for leak, e.g in the viewDidUnload method , you are not releasing any instance member properly.

    you need to call release on the object which you either alloced, init or retain.

     (void)viewDidUnload {
        // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
        // For example: self.myOutlet = nil;
    
        [remoteRecipientItems release];
        remoteRecipientItems = nil;
        [remoteRecipientID release];
        remoteRecipientID = nil;
         ..................
        ..................
    
    }
    

    Would suggest you to spend some time to read Memory Management Programming Guide

    0 讨论(0)
  • 2021-01-27 18:40

    Please see my response here: Deallocating and removing UiButtons

    It contains an explanation of when you should retain/release or rely on autorelease. Apply these rules to your code above. In short, anytime you alloc anything, you should be following it with a call to release on that object. If you don't, you'll have a memory leak.

    Update:

    You're not releasing the activityIndicator that you create here.

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        // ...
    
        activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        // ...
        [self.view addSubview:activityIndicator];
    }
    
    0 讨论(0)
提交回复
热议问题