在Instruments中进行性能分析时main.m中的内存泄漏? [英] Memory Leaks in main.m while profiling in Instruments?
问题描述
在我的应用程序之一中,我正在UIKit,UIFoundation和QuartzCore中发生内存泄漏.当我去调用树时,它在main.m
中显示泄漏.我真的不知道为什么会这样.您可以在下面看到内存泄漏的屏幕快照.
In my one of the app i am getting memory leak in UIKit,UIFoundation and QuartzCore. When i go for call tree it showing leak in main.m
. I really didn't have any clue why is this happening. You can see the screen shots of the memory leak below.
在通话树中
如何解决此泄漏?
内存泄漏代码
- (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)];
}
非常感谢您的帮助.
推荐答案
最后,我通过将选择器视图allocation/initialisation
部分从方法showCustomePrices
移到了viewwillAppear
,解决了我的问题.真棒,没有任何内存泄漏.
Finally i solved my problem by moving the picker view allocation/initialisation
part from the method showCustomePrices
to viewwillAppear
. Which works awesome without any memory leaks.
以前发生的事情是,每当我单击按钮时,它就会弹出带有内存分配的pickerview
.那就是为什么发生内存泄漏.
What is happened before is whenever i click the button it popups the pickerview
with memory allocation. Thats why the memory leak is happened.
现在,移入viewwillAppear
后,它只是在第一次加载视图时分配.然后在没有任何内存分配的情况下访问Picker View.这样可以消除内存泄漏.
Now after moving in to viewwillAppear
it just allocating on first time when the view gets loaded. Then Picker View is accessed without any memory allocation. So memory leaks is removed.
这篇关于在Instruments中进行性能分析时main.m中的内存泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!