[英]presentModalViewController memory leak or EXC_BAD_ACCESS
[英]calling presentModalViewController causes “EXC_BAD_ACCESS”
我正在創建一個iPad應用程序。 在其中,我設置了一個UITabBarController來顯示3個視圖。 查看1,查看2和查看3。這一切都很好。 在視圖1上,用戶正在創建訂單。 然后,他們觸摸建立訂單的按鈕。 這在模式視圖中顯示,允許用戶在實際發送之前對其進行查看。 他們可以“提交”或“編輯”訂單,無論哪種方式,我都關閉了模態並返回到視圖1。這也很好。 但是,如果用戶再次觸摸“ make”命令按鈕,則這次加載模式視圖會導致崩潰“ EXC_BAD_ACCESS”。 我復制了代碼,就像在應用程序中另一個模式視圖一樣復制了代碼,這一次又一次地顯示自己沒有問題。 我現在很困惑,不勝感激。 謝謝。 調用模式的代碼是:
-(IBAction) makeOrder {
NSMutableArray *orderItems = [[NSMutableArray alloc] init];
//code that populates orderItems array - removed for brevity
NSLog(@"order items count:%d", [orderItems count]);
// Create the modal view controller
PartsOrderViewController *modalController = [[PartsOrderViewController alloc] initWithNibName:@"PartsOrderView" bundle:nil];
//this is the only difference b/w this and the other modal view. The other
//modal presents as a formsheet
modalController.modalPresentationStyle = UIModalPresentationFullScreen;
modalController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
modalController.orderList = orderItems;
modalController.storeId = selectedCustomer.storeID;
modalController.customerInfo = customerInfo.text;
modalController.customerTamsId = selectedCustomer.customerTAMSID;
// show the Controller modally -- This is the line that cause the error after the second time
[self presentModalViewController:modalController animated:YES];
// Clean up resources
[modalController release];
}
它實際上進入模態的viewDidLoad中,但是一旦運行完畢便崩潰。
這是模態的代碼:
#import "PartsOrderViewController.h"
@implementation PartsOrderViewController
@synthesize customerTamsId;
@synthesize customerInfo;
@synthesize invoiceDate;
@synthesize invoiceTime;
@synthesize storeId;
@synthesize customerInfoLabel;
@synthesize invoiceDateLabel;
@synthesize invoiceTimeLabel;
@synthesize storeIdLabel;
@synthesize orderList;
@synthesize delegate;
#pragma mark -
#pragma mark View methods
-(IBAction) editOrder {
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction) submitOrder {
//code removed for brevity
}
#pragma mark -
#pragma mark View implementation methods
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
self.customerInfoLabel.text = self.customerInfo;
self.storeIdLabel.text = self.storeId;
self.invoiceDateLabel.text = self.invoiceDate;
self.invoiceTimeLabel.text = self.invoiceTime;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Overriden to allow any orientation.
return NO;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
@end
更新:找到解決方案:令人反感的代碼被標記為-
-(NSMutableArray *)buildOrderList {
NSMutableArray *orderItems = [[NSMutableArray alloc] init];
id cellObject = NULL;
int counter = 0;
NSEnumerator *theEnum = [self.partsList objectEnumerator];
while((cellObject = [theEnum nextObject]) != NULL)
{
GridTableCell *cell = (GridTableCell *)[self.partsListGrid cellForRowAtIndexPath:[NSIndexPath indexPathForRow:counter inSection:0]];
UILabel *lineAbbrev = (UILabel *)[cell.contentView.subviews objectAtIndex:0];
UILabel *partNo = (UILabel *)[cell.contentView.subviews objectAtIndex:1];
UITextView *orderQty = (UITextView *)[cell.contentView.subviews objectAtIndex:3];
//NSLog(@"OrderQty length: %d", [orderQty.text length]);
//NSLog(@"Part#:%@, OrderQty:%@", partNo.text, orderQty.text);
PartOrderIn *invItem = [[PartOrderIn alloc] init];
invItem.lineAbbrev = lineAbbrev.text;
invItem.partNumber = partNo.text;
invItem.orderQty = orderQty.text;
invItem.partMessage = @"";
if ([invItem.orderQty length] > 0) {
[orderItems addObject:invItem];
}
counter++;
[invItem release];
//The following three lines is what was killing it
//[lineAbbrev release];
//[partNo release];
//[orderQty release];
}
//NSLog(@"order items count:%d", [orderItems count]);
return orderItems;
}
冒着陳述明顯的(抱歉;)的風險,您是否已通過調試器逐步執行此操作? 錯誤的訪問可能是內存分配問題(再次,很明顯,先生)。 如何定義屬性(是否保留orderList?其他屬性?)。 使用調試器中的表達式或按內存地址檢查崩潰的位置並記下屬性值。 我的猜測是您假設保留了某些未保留的東西。
沒有什么立即跳出來的(問題很可能是為簡潔起見而刪除的代碼),但是您是否嘗試啟用僵屍程序? 如何啟用僵屍。 這通常會給您一些有關犯罪者的提示,或者至少會給您一些提示。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.