开发者

NSArray causing memory leak when simulated low memory warning

开发者 https://www.devze.com 2023-03-08 22:55 出处:网络
I have made a sample project that reproduces this issue which contains two views: root header: #import <UIKit/UIKit.h>

I have made a sample project that reproduces this issue which contains two views:

root header:

#import <UIKit/UIKit.h>
#import "view2.h"

@interface RootViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>{
    view2 *secondView;
    UITableView *table;
    NSArray *array;
}

@property (nonatomic, retain) view2 *secondView;
@property (nonatomic, retain) IBOutlet UITableView *table;
@property (nonatomic, retain) NSArray *array;
@end

root main:

#import "RootViewController.h"

@implementation RootViewController
@synthesize table, array, secondView;

- (void)viewDidLoad
{
    [super viewDidLoad];    
    if(self.array == nil){
        self.array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];
    }
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    table = nil;
    array = nil;
    secondView = nil;

}

- (void)dealloc
{
    [table release];
    [array release];
    [secondView release];
    [super dealloc];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }

    cell.textLabel.text = [array objectAtIndex:indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (secondView == nil) {
        secondView = [[view2 alloc] init];
    }
    [self.navigationController pushViewController:secondView animated:YES]; 
}
@end

view2 simple contains a label with the text "view 2" for identification purposes. All this code is doing in the root contro开发者_运维知识库ller is creating an array with the values 1,2,3,4 and binding this text as rows to the table, clicking any row pushes view 2 onto the stack. if you load up the app in the simulator using the leaks instruments tool, click on any row so that view2 is displayed and then simulate an error warning the following leaks appear: image for the line:

self.array = [NSArray arrayWithObjects:@"1", @"2", @"3", @"4", nil];

this is causing me a lot of problems in my main app as i am using arrays to provide data in tables all over the place.

i have tried various ways to fix this such as declaring the array in different ways to no avail.

any help is greatly appreciated!

thanks


In viewDidUnload you're mixing up property vs. direct ivar access.

array = nil simply sets the ivar to nil without using the synthesized accessor method. You have to use the dot notation: self.array = nil;

This way the accessor setArray: is used which handles memory management for you.

Mixing up ivars and properties is a frequent problem amongst Objective-C beginners. The confusion can easily be prevented by always using different names for properties and ivars:

@synthesize array = _array;

You can just leave out the ivar declaration in the class's @interface or name it as in the @synthesize directive.

0

精彩评论

暂无评论...
验证码 换一张
取 消