Xcode 4.2.1: UIPickerView causes memory leak using ARC

For one of my last school projects, I am creating an iPad / iPhone application. For several days I have been working on a problem with a specific memory leak. My application runs on a specific view controller (VCMainStatistics_iPad). From there I click another view controller (VCSocialMedia_iPad). Subsequently, I return to the first view controller.

When I repeat this sequence, I notice (using the tools - Activity Monitor) that the memory usage in the application continues to grow. By disabling parts of the code, I eventually found out that it had something to do with pickerView. This code does not leak:

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return 0; } 

However, when I increase the number of rows, leaks begin to occur (approximately 0.07 MB per row). Obviously, this is why I believe pickerView is causing leaks. I tried to remove subviews from pickerView before freeing by setting pickerView to nil and more ... nothing fixes the problem. To hope to make things a little clearer, I'll post some more code.

Header file:

 #import "UniversalViewController.h" #define DATATYPE_SOCIALMEDIA 0 @interface VCSocialMedia_iPad : UniversalViewController <UIPickerViewDataSource, UIPickerViewDelegate> { NSArray *lMediaTypes; NSMutableArray *lMediaData; __weak IBOutlet UIPickerView *pkSocialMedia; __weak IBOutlet UILabel *lblGraph; } @end 

PickerView delegate methods:

 - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component { // Get key of requested row NSString *title = [[lMediaTypes objectAtIndex:row] capitalizedString]; // Capitalize first letter title = [title capitalizedString]; // Return return title; } - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { // Make or clear data lists if( lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] == nil ){ lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] = [[NSMutableArray alloc] init]; } else{ [lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] removeAllObjects]; } if( lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] == nil ){ lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] = [[NSMutableArray alloc] init]; } else{ [lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] removeAllObjects]; } // Get required key NSString *dictKey = [lMediaTypes objectAtIndex:row]; if( [dictKey isEqualToString:@"total_views"] ){ return; } // Adjust graph label lblGraph.text = [NSString stringWithFormat:@"Stats from %@", dictKey]; // Get data of selected row NSArray *mediaData = [lMediaData objectAtIndex:row]; // Add each day to data lists: inversed order for( int day = [mediaData count]-1; day >= 0; day-- ){ NSDictionary *dayData = [mediaData objectAtIndex:day]; dictKey = @"wpsd_trends_date"; NSString *date = [dayData objectForKey:dictKey]; // Remove 00:00:00 date = [date stringByReplacingOccurrencesOfString:@" 00:00:00" withString:@""]; [lGraphDayDataX[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] addObject:date]; dictKey = @"wpsd_trends_stats"; NSString *stats = [dayData objectForKey:dictKey]; [lGraphDayDataY[iSelectedServerIndex][DATATYPE_SOCIALMEDIA] addObject:stats]; } // Update the graphs [self updateGlobalScreen]; } 

PickerView Data Collection Methods:

 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return [lMediaTypes count]; } 

Deallocation:

 - (void)dealloc { pkSocialMedia = nil; lblGraph = nil; lMediaData = nil; lMediaTypes = nil; } 

I just recently converted the project to Objective-C ARC, so there is a good chance this problem has something to do with my lack of experience with the concept. Also, this is also my first Xcode project. Hope someone here can help: please let me know if I need to post more code to clarify the situation.

Thanks in advance!

+4
source share
3 answers

No solution was ever found, so I used a workaround: replacing the NSPickerView with the NSTableView component, the leak no longer occurred. For everyone who noticed a problem and tried to find a solution: thanks for trying!

0
source

Try removing the -(void)dealloc . It should not be implemented when using ARC. If you are not using ARC, it needs to call [super dealloc] .

0
source

I have the same problem. This only happens when the UIPickerView is out of bounds. The way I fixed this should never go beyond the limits of a UIPickerView (just fade and fade to show / hide a UIPickerView). Probably a bug in UIKit.

0
source

Source: https://habr.com/ru/post/1386511/


All Articles