Obj-C UIActionSheet 内存泄漏 iPhone
我正在编写一个弹出屏幕,如果设置中的 mulValue
值为 nil
,该屏幕就会显示。这是代码:
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize myHelpfile = ivHelpfile; //UIAlertView
@synthesize myAboutfile = ivAboutfile; //UIAlertView
@synthesize myNoarea = ivNoarea; //UIAlertView
@synthesize myMap = ivMap; //UIImageView
@synthesize myAreapick = ivAreapick; //UIActionSheet
@synthesize myAdvancedmode = ivAdvancedmode; //NSString
@synthesize myAreaset = ivAreaset; //NSString
@synthesize myAreaarray = ivAreaarray; //NSArray
@synthesize appSettingsViewController, settingsViewController;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Wildlife";
NSString *area = [[NSUserDefaults standardUserDefaults] stringForKey:@"mulValue"];
[self setMyAreaset:area];
if ([self myAreaset] == nil) { //check if any region is selected. If not, push UIActionsheet
NSArray *array = [[NSArray alloc] initWithObjects: // creating array for different regions
[NSString stringWithString:@"Region 1"],
[NSString stringWithString:@"Region 2"],
[NSString stringWithString:@"Region 3"],
[NSString stringWithString:@"Region 4"],
[NSString stringWithString:@"Region 5"],
[NSString stringWithString:@"Region 6"],nil];
[self setMyAreaarray:array];
[array release], array = nil;
NSLog(@"Built areaselectArray");
NSLog(@"Values of areaselectArray are %@",[self myAreaarray]);
UIActionSheet *areaselect = [[UIActionSheet alloc] initWithTitle:@"Select your current area" //creation of popup Area Selection
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[self setMyAreapick:areaselect];
[areaselect release], areaselect = nil;
int countarray = [[self myAreaarray] count];
for (int i = 0; i < countarray; i++) { //building list from array
[[self myAreapick] addButtonWithTitle:[[self myAreaarray] objectAtIndex:i]];
}
[[self myAreapick] addButtonWithTitle:@"Cancel"];
[self myAreapick].cancelButtonIndex = countarray;
[[self myAreapick] showInView:self.view];//show the general UIActionSheet instance
NSLog(@"Out of viewDidLoad");
}
else {
NSLog(@"Area is %@, no need for areaselectArray",area);
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger )buttonIndex
{
int countarray = [[self myAreaarray] count];
if (buttonIndex != countarray) { //cancel button is at countarray, so for anything that is not cancel, do:
NSString *area = [[self myAreaarray] objectAtIndex:(buttonIndex)];
[self setMyAreaset:area];
[[NSUserDefaults standardUserDefaults] setObject:[self myAreaset] forKey:@"mulValue"];
[[NSUserDefaults standardUserDefaults] synchronize]; //sync
NSLog(@"Released areaselectArray");
}
else {
}
}
在我的 FirstViewController.h 中,我有:
@interface FirstViewController : UIViewController <UIActionSheetDelegate, IASKSettingsDelegate, UIAlertViewDelegate> {
UIAlertView *ivHelpfile;
UIAlertView *ivAboutfile;
UIAlertView *ivNoarea;
UIActionSheet *ivAreapick;
UIImageView *ivMap;
NSString *ivAdvancedmode;
NSString *ivAreaset;
NSArray *ivAreaarray;
}
@property(nonatomic, retain) UIAlertView *myHelpfile;
@property(nonatomic, retain) UIAlertView *myAboutfile;
@property(nonatomic, retain) UIAlertView *myNoarea;
@property(nonatomic, retain) UIActionSheet *myAreapick;
@property(nonatomic, retain) UIImageView *myMap;
@property(nonatomic, copy) NSString *myAdvancedmode;
@property(nonatomic, copy) NSString *myAreaset;
@property(nonatomic, retain) NSArray *myAreaarray;
当我在运行 Instruments 的模拟器中运行我的应用程序时,每当滚动行列表时都会出现内存泄漏(由 [self myAreaarray]< 构建) /code>) 并松开手指。奇怪的是,此时它还没有真正做任何其他事情。主视图已加载,但奇怪的是,这会导致滚动列表时出现内存泄漏。
我在 Instruments 中遇到的泄漏如下:
_NSCFType 48 bytes CoreGraphics CGTypeCreateInstanceWithAllocator
UIDeviceWhiteColor 16 bytes UIKit +[UIColor allocWithZone:]
当我再次滚动列表时,会出现更多错误。看起来我正在分配一些东西并且没有释放它(当我滚动列表时?),但我现在找不到它。
任何帮助将不胜感激!
I'm coding a popup screen that will show up if a the value for mulValue
in the settings is nil
. Here is the code:
#import "FirstViewController.h"
@implementation FirstViewController
@synthesize myHelpfile = ivHelpfile; //UIAlertView
@synthesize myAboutfile = ivAboutfile; //UIAlertView
@synthesize myNoarea = ivNoarea; //UIAlertView
@synthesize myMap = ivMap; //UIImageView
@synthesize myAreapick = ivAreapick; //UIActionSheet
@synthesize myAdvancedmode = ivAdvancedmode; //NSString
@synthesize myAreaset = ivAreaset; //NSString
@synthesize myAreaarray = ivAreaarray; //NSArray
@synthesize appSettingsViewController, settingsViewController;
- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Wildlife";
NSString *area = [[NSUserDefaults standardUserDefaults] stringForKey:@"mulValue"];
[self setMyAreaset:area];
if ([self myAreaset] == nil) { //check if any region is selected. If not, push UIActionsheet
NSArray *array = [[NSArray alloc] initWithObjects: // creating array for different regions
[NSString stringWithString:@"Region 1"],
[NSString stringWithString:@"Region 2"],
[NSString stringWithString:@"Region 3"],
[NSString stringWithString:@"Region 4"],
[NSString stringWithString:@"Region 5"],
[NSString stringWithString:@"Region 6"],nil];
[self setMyAreaarray:array];
[array release], array = nil;
NSLog(@"Built areaselectArray");
NSLog(@"Values of areaselectArray are %@",[self myAreaarray]);
UIActionSheet *areaselect = [[UIActionSheet alloc] initWithTitle:@"Select your current area" //creation of popup Area Selection
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[self setMyAreapick:areaselect];
[areaselect release], areaselect = nil;
int countarray = [[self myAreaarray] count];
for (int i = 0; i < countarray; i++) { //building list from array
[[self myAreapick] addButtonWithTitle:[[self myAreaarray] objectAtIndex:i]];
}
[[self myAreapick] addButtonWithTitle:@"Cancel"];
[self myAreapick].cancelButtonIndex = countarray;
[[self myAreapick] showInView:self.view];//show the general UIActionSheet instance
NSLog(@"Out of viewDidLoad");
}
else {
NSLog(@"Area is %@, no need for areaselectArray",area);
}
}
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger )buttonIndex
{
int countarray = [[self myAreaarray] count];
if (buttonIndex != countarray) { //cancel button is at countarray, so for anything that is not cancel, do:
NSString *area = [[self myAreaarray] objectAtIndex:(buttonIndex)];
[self setMyAreaset:area];
[[NSUserDefaults standardUserDefaults] setObject:[self myAreaset] forKey:@"mulValue"];
[[NSUserDefaults standardUserDefaults] synchronize]; //sync
NSLog(@"Released areaselectArray");
}
else {
}
}
And in my FirstViewController.h I have:
@interface FirstViewController : UIViewController <UIActionSheetDelegate, IASKSettingsDelegate, UIAlertViewDelegate> {
UIAlertView *ivHelpfile;
UIAlertView *ivAboutfile;
UIAlertView *ivNoarea;
UIActionSheet *ivAreapick;
UIImageView *ivMap;
NSString *ivAdvancedmode;
NSString *ivAreaset;
NSArray *ivAreaarray;
}
@property(nonatomic, retain) UIAlertView *myHelpfile;
@property(nonatomic, retain) UIAlertView *myAboutfile;
@property(nonatomic, retain) UIAlertView *myNoarea;
@property(nonatomic, retain) UIActionSheet *myAreapick;
@property(nonatomic, retain) UIImageView *myMap;
@property(nonatomic, copy) NSString *myAdvancedmode;
@property(nonatomic, copy) NSString *myAreaset;
@property(nonatomic, retain) NSArray *myAreaarray;
When I run my app in the simulator with Instruments running, I get a memory leak whenever I scroll the list of rows (as built by [self myAreaarray]
) and release my finger. The weird thing is, it has not really done anything else at that point. The main view is loaded, but it would be strange that that would cause a memory leak based on scrolling through the list.
The leaks I get in Instruments are the following:
_NSCFType 48 bytes CoreGraphics CGTypeCreateInstanceWithAllocator
UIDeviceWhiteColor 16 bytes UIKit +[UIColor allocWithZone:]
When I scroll through the list again, more of those errors show up. It looks like I'm allocating something and not releasing it (while I'm scrolling the list?), but I can't find it at this point.
Any help would be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也看到了这一点,甚至从中抽象出所有应用程序代码:
当通过 Instruments 内存泄漏分析器运行时,操作表的每次显示都会产生 6 个 UIDeviceWhiteColor 泄漏和 4 个 CGColor 泄漏。在我看来,这是 UIKit 中的一个错误。
I'm seeing it as well, even abstracting all your app code from it:
When running through Instruments memory leak profiler, each display of the action sheet yieds 6 UIDeviceWhiteColor leaks and 4 CGColor leaks. Looks to me like this is a bug in UIKit.