【问题标题】:Obj-C UIActionSheet Memory Leaks iPhoneObj-C UIActionSheet 内存泄漏 iPhone
【发布时间】:2011-03-08 03:41:25
【问题描述】:

我正在编写一个弹出屏幕,如果设置中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] 构建)并松开手指时,都会出现内存泄漏。奇怪的是,它当时并没有真正做任何其他事情。主视图已加载,但奇怪的是,这会导致基于滚动列表的内存泄漏。

我在 Instruments 中得到的泄漏如下:

_NSCFType 48 字节 CoreGraphics CGTypeCreateInstanceWithAllocator

UIDeviceWhiteColor 16 字节 UIKit +[UIColor allocWithZone:]

当我再次滚动列表时,会出现更多错误。看起来我正在分配一些东西而不是释放它(当我滚动列表时?),但我现在找不到它。

任何帮助将不胜感激!

【问题讨论】:

    标签: iphone objective-c arrays memory-leaks uiactionsheet


    【解决方案1】:

    我也看到了,甚至从中抽象出你所有的应用代码:

     UIActionSheet *as = [[UIActionSheet alloc] initWithTitle:@"Test" delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
    
     for (int i = 0; i < 100; i++) {
         [as addButtonWithTitle:[NSString stringWithFormat:@"Button %i", i]];
     }
     as.cancelButtonIndex = 99;
     as.destructiveButtonIndex = 0;
    
     [as showInView:self.view];
     [as release];
    

    通过 Instruments 内存泄漏分析器运行时,操作表的每个显示都会产生 6 个 UIDeviceWhiteColor 泄漏和 4 个 CGColor 泄漏。在我看来这是 UIKit 中的一个错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-11-23
      • 2011-12-10
      • 2011-05-24
      • 2023-03-20
      • 1970-01-01
      相关资源
      最近更新 更多