在我的应用程序中,我在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)];
}
非常感谢任何帮助。
答案 0 :(得分:1)
如果您的项目不是ARC,可能是因为您已将[super dealloc];
留在某处继承基础类。我对NSObject
的子类有同样的问题。我忘了写[super dealloc];
并得到了一些类似的泄漏。
答案 1 :(得分:1)
最后,我通过将方法allocation/initialisation
中的选择器视图showCustomePrices
部分移动到viewwillAppear
来解决了我的问题。哪个工作真棒,没有任何内存泄漏。
之前发生的事情是每当我点击按钮时,它会弹出pickerview
内存分配。这就是发生内存泄漏的原因。
现在进入viewwillAppear
后,它只是在视图加载时第一次分配。然后在没有任何内存分配的情况下访问Picker View。因此消除了内存泄漏。
答案 2 :(得分:0)
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
在你的main.m中试试这个
答案 3 :(得分:-1)
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
int retVal = UIApplicationMain(argc, argv, @"yourAppName", nil);
[pool release];
return retVal;
}