簡體   English   中英

在使用儀器進行分析時,main.m中的內存泄漏?

[英]Memory Leaks in main.m while profiling in Instruments?

在我的應用程序中,我在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)];

}

任何幫助深表感謝。

如果你的項目不是ARC,可能是因為你離開了[super dealloc]; 在某處繼承基礎類。 我對NSObject的子類有同樣的問題。 我忘了寫[super dealloc]; 並得到一些類似的泄漏。

最后,我通過將showCustomePrices視圖allocation/initialisation部分從方法showCustomePricesviewwillAppear來解決我的問題。 哪個工作真棒,沒有任何內存泄漏。

之前發生的事情是,每當我點擊按鈕時,它會彈出帶內存分配的選擇器pickerview 這就是內存泄漏發生的原因。

現在進入viewwillAppear它只是在視圖加載時第一次分配。 然后在沒有任何內存分配的情況下訪問Picker View。 因此消除了內存泄漏。

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

在你的main.m中嘗試這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM