3

在我的一个应用程序中,我在 UIKit、UIFoundation 和 QuartzCore 中遇到内存泄漏。当我去调用树时,它显示泄漏main.m。我真的不知道为什么会这样。您可以在下面看到内存泄漏的屏幕截图。在此处输入图像描述

在调用树中

在此处输入图像描述

如何解决这种泄漏?

内存泄漏代码

- (void) showCustomPrices
{

int priceValue = 0;
NSArray* priceSplitValue = [btnpriceButton.titleLabel.text componentsSeparatedByString: @"."];
NSString* priceFinal = [priceSplitValue objectAtIndex:0];

for(int i=0;i<[priceArray count];i++)
{ 
    if([[priceArray objectAtIndex:i] isEqualToString:priceFinal]){
        priceValue = i; 
    }
}


    //Assign the cocktail to picker view
    priceActionsheet = [[UIActionSheet alloc] initWithTitle:nil
                                              delegate:self
                                     cancelButtonTitle:nil
                                destructiveButtonTitle:nil
                                     otherButtonTitles:nil];//as we want to display a subview we won't be using the default buttons but rather we're need to create a toolbar to display the buttons on

    [priceActionsheet setActionSheetStyle:UIActionSheetStyleBlackTranslucent];

    CGRect pickerFrame = CGRectMake(0, 40, 0, 0);

    pricePickerview = [[UIPickerView alloc] initWithFrame:pickerFrame];
    pricePickerview.showsSelectionIndicator = YES;
    pricePickerview.dataSource = self;
    pricePickerview.delegate = self;


    [priceActionsheet addSubview:pricePickerview];
    //[pickerView release];

    priceToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 485, 44)];
    priceToolbar.barStyle = UIBarStyleBlackTranslucent;
    [priceToolbar sizeToFit];

    NSMutableArray *barItems = [[NSMutableArray alloc] init];

    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];

    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
    [barItems addObject:doneBtn];

    UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
    [barItems addObject:cancelBtn];

    [pricePickerview selectRow:priceValue inComponent:0 animated:YES];//Memory leaks shows 100% here

    [priceToolbar setItems:barItems animated:YES];

    [priceActionsheet addSubview:priceToolbar];

    [priceActionsheet addSubview:pricePickerview];

    [priceActionsheet showInView:self.view];  

    [priceActionsheet setBounds:CGRectMake(0, 0, 485, 320)];

}

任何帮助深表感谢。

4

4 回答 4

1

如果您的项目不是 ARC,可能是因为您在[super dealloc];某个地方留下了基础类的子类。我的子类也有同样的问题NSObject。我忘了写[super dealloc];,并且得到了一些类似的泄漏。

于 2012-11-30T17:24:46.930 回答
1

最后,我通过将选择器视图allocation/initialisation部分从方法移动showCustomePricesviewwillAppear. 没有任何内存泄漏,效果很棒。

之前发生的事情是每当我单击按钮时,它都会弹出pickerview内存分配。这就是发生内存泄漏的原因。

现在,在进入viewwillAppear它之后,只需在第一次加载视图时进行分配。然后在没有任何内存分配的情况下访问 Picker View。所以内存泄漏被删除。

于 2012-12-04T09:08:59.870 回答
0
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;

在你的 main.m 中试试这个

于 2012-12-04T11:37:57.047 回答
-1
int main(int argc, char *argv[])
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    int retVal = UIApplicationMain(argc, argv, @"yourAppName", nil);
    [pool release];
    return retVal;
}
于 2012-12-04T12:09:35.710 回答