2012-11-30 34 views
3

在我的一个应用程序中,我得到了UIKit,Uifoundation和QuartzCore中的内存泄漏。当我去呼叫树显示泄漏main.m。我真的没有任何线索,为什么会发生这种情况。您可以在下面看到内存泄漏的屏幕截图。 enter image description here内存泄漏在main.m而在仪器分析?

在调用树

enter image description here

如何解决这个泄漏?

内存泄漏CODE

- (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)]; 

}

任何帮助深表感谢。

+0

如果您单击第一个图中的对象的地址旁边的小箭头,它会告诉你的保留/释放历史。我发现关于对象的最佳线索。 –

+0

@PhillipMills编辑我的问题,请看看它。谢谢 – GoCrazy

+0

在顶部栏中有3个“查看”按钮。选择最右边的一个来查看堆栈跟踪,并查看是否有来自您的应用程序代码的方法调用。 –

回答

1

最后我解决了我的问题,将选取器视图allocation/initialisation部分从方法showCustomePrices移到viewwillAppear。哪个工作真棒没有任何内存泄漏。

之前发生的事情是,每当我点击按钮弹出pickerview与内存分配。这就是为什么发生内存泄漏。

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

+0

这很奇怪。我在我的项目中使用了UIPickerView,并且在每次推送UIButton时分配+ init。但我没有泄漏。 –

+0

是真的有线。我已经在不同的项目中使用过相同的工作,但不在 – GoCrazy

+0

中,如果一次分配对您有用,我认为最好是在viewDidLoad()而不是viewWillAppear()中使用它。 –

1

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

+0

感谢您的回答..我在ARC中创建项目,并从非ARC项目中复制了一些项目。我不确定如何处理您的回答 – GoCrazy

+0

您确定项目的非ARC部分没有泄漏吗? –

+0

我希望如此,但具体如何检查? – GoCrazy

0
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; 
    int retVal = UIApplicationMain(argc, argv, nil, nil); 
    [pool release]; 
    return retVal; 

试试这个在您的main.m

-1
int main(int argc, char *argv[]) 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    int retVal = UIApplicationMain(argc, argv, @"yourAppName", nil); 
    [pool release]; 
    return retVal; 
}