简体   繁体   中英

Using EKEventEditViewController causing a memory leak?

I am using an instance of EKEventEditViewController so I can edit a selected event. When the controller is dismissed via eventEditViewController:didCompleteWithAction: I notice that my memory usage does not go down. Each subsequent use of the EventEditViewController increases my memory usage and it never goes back down. My app starts out having used 4.2mb of memory. If all I do is select a cell in my tableview, present the event edit controller, dismiss the event edit controller and repeat the process multiple times, the memory amount used on my device increases to over 10mb.

Can someone tell me if there is something I am doing wrong? It seems like I am leaking the memory. I am using ARC and have a strong pointer to a property of type EKEventEditViewController. When I go to present the controller, I re-alloc/init the controller, which should release the previous reference correct?

@interface HZRunwayViewController ()
@property (strong, nonatomic) EKEventEditViewController *eventEditViewController;
@end


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.viewOptions.selectedSegmentIndex == 2) {
        if (indexPath.section == 0 && [self.eventsForCurrentDay count]) {
            id cell = [tableView cellForRowAtIndexPath:indexPath];
            if ([cell isKindOfClass:[HZEventTableViewCell class]]) {

                HZEventTableViewCell *eventCell = (HZEventTableViewCell *)cell;
                if (eventCell ) {
                    EKEvent *event = eventCell.event;
                    self.eventEditViewController = [[EKEventEditViewController alloc] init];
                    self.eventEditViewController.eventStore = self.calendarData.eventStore;
                    self.eventEditViewController.editViewDelegate = self;
                    self.eventEditViewController.event = event;
                    [self presentViewController:self.eventEditViewController animated:YES completion:nil];
                }
            }
        }
    }
}



- (void)eventEditViewController:(EKEventEditViewController *)controller didCompleteWithAction:(EKEventEditViewAction)action {
    [self updateDayLogic];
    [self dismissViewControllerAnimated:YES completion:nil];
}

As a side note, the updateDayLogic method wasn't included because I tested it with a UIButton, calling it several times and my memory usage only increased 0.2mb after several invocations. The issue seems to stem from the EKEventEditViewController

So, I revised my didSelectRowAtIndexPath method as shown below. I check to see if the EKEventEditViewController is nil and if so, initialize it and set it up. If it has already been instanced, then re-use it.

I am not sure why re-allocating/initializing the property caused the memory to increase. It should automatically free up the memory due to the fact that my code is no longer strongly pointing to the view controller. Either way, I managed to fix the memory increase issue by doing it as shown below.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.viewOptions.selectedSegmentIndex == 2) {
        if (indexPath.section == 0 && [self.eventsForCurrentDay count]) {
            id cell = [tableView cellForRowAtIndexPath:indexPath];
            if ([cell isKindOfClass:[HZEventTableViewCell class]]) {

                HZEventTableViewCell *eventCell = (HZEventTableViewCell *)cell;
                if (eventCell ) {
                    EKEvent *event = eventCell.event;

                    // Check if the EKEventEditViewController is nil
                    if (!self.eventEditViewController) {
                        self.eventEditViewController = [[EKEventEditViewController alloc] init];
                        self.eventEditViewController.eventStore = self.calendarData.eventStore;
                        self.eventEditViewController.editViewDelegate = self;
                    }
                    self.eventEditViewController.event = event;
                    [self presentViewController:self.eventEditViewController animated:YES completion:nil];
                }
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM