好吧,我终于到了测试我的iPad应用程序的地步......iphone内存泄漏和malloc?
我的应用程序所做的一件事是在滚动视图中显示一个大的(2mb)图像。这导致iPad获得内存警告。我在仪器中运行应用程序来检查泄漏。
当我加载图像,检测到泄漏,我看到的分配执行以下操作:
所有分配:83.9 MB malloc的48.55 MB:48.55 MB malloc的34.63 MB:34.63 MB
什么即时通讯试图了解如何明显堵塞泄漏,但也为什么一个2MB的图像导致一个20x大小的malloc
我是非常新的obj-c编程,所以我确信这是一个明显的事情,但我不知道。下面是代码:
@interface ChartsViewController : UIViewController <UIScrollViewDelegate, UIPickerViewDelegate, UIPickerViewDataSource> {
IBOutlet UIScrollView *scrollView;
UIImageView *imageView;
NSString *chart;
NSString *chartFile;
UIPickerView *picker;
NSDictionary *chartsDictionary;
NSArray *chartTypes;
NSArray *charts;
IBOutlet UILabel *chartNameLabel;
IBOutlet UIActivityIndicatorView *activityIndicator;
}
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) UIImageView *imageView;
@property (nonatomic, retain) NSString *chart;
@property (nonatomic, retain) NSString *chartFile;
@property (nonatomic, retain) IBOutlet UIPickerView *picker;
@property (nonatomic, retain) NSDictionary *chartsDictionary;
@property (nonatomic, retain) NSArray *chartTypes;
@property (nonatomic, retain) NSArray *charts;
@property (nonatomic, retain) IBOutlet UILabel *chartNameLabel;
@property (nonatomic, retain) IBOutlet UIActivityIndicatorView *activityIndicator;
-(IBAction) chartSelected;
- (void)alertView:(UIAlertView *)actionSheet
///////////////////////////////
-(IBAction) chartSelected {
[imageView removeFromSuperview];
imageView = nil;
chartNameLabel.text = @"";
NSInteger chartTypeRow = [picker selectedRowInComponent:kChartTypeComponent];
NSInteger chartRow= [picker selectedRowInComponent:kChartComponent];
chart = [self.charts objectAtIndex:chartRow];
chartFile = [chart stringByReplacingOccurrencesOfString:@" " withString:@"_"];
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *docsPath = [paths objectAtIndex:0];
NSString *tempString = [[NSString alloc]initWithFormat:@"%@/%@.jpg",docsPath,chartFile];
NSData *temp = [NSData dataWithContentsOfFile:tempString];
if (temp != NULL){
temp = nil;
[imageView removeFromSuperview];
imageView = nil;
UIImageView *tempImage = [[UIImageView alloc]initWithImage:[UIImage imageWithContentsOfFile: tempString]];
[tempString release];
self.imageView = tempImage;
scrollView.contentSize = CGSizeMake(imageView.frame.size.width , imageView.frame.size.height);
scrollView.maximumZoomScale = 4.0;
scrollView.minimumZoomScale = .05;
scrollView.clipsToBounds = YES;
scrollView.delegate = self;
scrollView.zoomScale = .3;
[scrollView addSubview:imageView];
[tempImage release];
imageView = nil;
chartNameLabel.text = chart;
}
else {
UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Download Chart"
message:@"It appears that you have not yet downloaded this chart. Press OK to download this chart to your iPad. Depending on your internet connection, the download could take several minutes."
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:@"Cancel", nil];
[alert show];
[alert release];
}
}
- (void)dealloc {
[imageView release];
[scrollView release];
[chartsDictionary release];
[picker release];
[chartTypes release];
[charts release];
[super dealloc];
}
缺少代码。 – 2010-04-23 04:42:18
你很快:)它现在有 – Brodie 2010-04-23 04:44:27
任何人都可以解释为什么它为2-3 MB图像分配20-30 MB? – Brodie 2010-04-23 05:09:47