I have a problem with a memory leak in a simple app. The code is taken from a book iPhone iOS Development Essentials. The code is as follows:
The h file
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
<UITableViewDelegate, UITableViewDataSource>
@property (strong, nonatomic) NSArray *colorNames;
@end
and the m file
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize colorNames;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.colorNames = [[NSArray alloc] initWithObjects:@"blue", @"red",@"green",@"yellow", nil];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
self.colorNames = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
} else {
return YES;
}
}
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.colorNames 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];
}
cell.textLabel.text = [self.colorNames objectAtIndex:[indexPath row]];
return cell;
}
@end
Every time when I try to scroll the table using the iPhone simulator I have a memory leak of 48k. Do you have any idea where the leak is?
Assuming that you don't use ARC
only if @property
colorNames
is a retain one you need to do for example
NSArray* cArray = [[NSArray alloc] initWithObjects:@"blue", @"red",@"green",@"yellow", nil];
self.colorNames = cArray;
[cArray release];
in addition autorelease
your cell once created.
if(cell==nil)
{
cell = [[[UITableViewCell alloc]
initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:CellIdentifier] autorelease];
}
Edit If you click on that memory leak, instruments can bring you to the specific line of code that creates a leak.
Hope it helps.
I have the same problem, already posted a bug report. Support answered me that they are aware of the problem, the Bug is still in open state and the ID is #10703036.
Still waiting even after the 4.3.2 update for Xcode...
Instead of @synthesize
colorNames
you should use:
@synthesize colorNames = _colorNames;
This creates an ivar names _colorNames
.
Now use:
_colorNames = [[NSArray alloc] initWithObjects:@"blue", @"red",@"green",@"yellow", nil];
The problem with using self.colorNames = [[NSArray ...
is that your property colorNames
gets double retained. Once by the attribute (strong) of you property and once by calling 'alloc'.
In viewDidUnload
you should use:
[_colorNames release];
_colorNames = nil;
Browsing different forums I've found the answer (I hope). Since the leak is from lib system_c.dlib and responsible frame strdup, people claim it's a bag in Apple library. The same problem was found with UIPickerView, UIDatePicker and UIScrollView controllers. The same size (48 Bytes), the same library and the same frame.
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.