2013-07-15 31 views
0

我有一個視圖控制器崩潰時,在導航視圖控制器中使用後退按鈕的問題。detailViewController崩潰dealloc由於屬性被釋放使用弧

在主表視圖控制器,我推翻爲賽格瑞像這樣製備:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { 

/* 
When a row is selected, the segue creates the detail view controller as the destination. 
Set the detail view controller's detail item to the item associated with the selected row. 
*/ 
if ([[segue identifier] isEqualToString:@"getHostedZoneSegue"]) { 

    NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow]; 
    GetHostedZoneViewController *detailViewController = [segue destinationViewController]; 
    NSLog(@"setting zone ID"); 

    detailViewController.zoneID = [hostedZonesListID objectAtIndex:selectedRowIndex.row]; 

} 
} 

GetHostedViewController有一個屬性了zoneid宣稱:

@interface GetHostedZoneViewController : UIViewController 
{ 
    NSString *zoneID; 
} 
@property (nonatomic, copy) NSString *zoneID; 
在viewDidLoad中

我執行該調用的方法中的框架(對框架的調用發生在GCD異步塊中,並且框架不使用ARC):

Route53GetHostedZoneRequest *request = 
[[Route53GetHostedZoneRequest alloc] initWithHostedZoneID:self.zoneID]; 

框架做它的事,像這樣: .H:

@interface Route53GetHostedZoneRequest : AmazonServiceRequestConfig 
{ 
    NSString *hostedZoneID; 
} 
@property (nonatomic, copy) NSString *hostedZoneID; 

.M:

@synthesize hostedZoneID; 

-(id)initWithHostedZoneID:(NSString *)theHostedZoneID 
{ 
    if (self = [self init]) { 
     hostedZoneID = theHostedZoneID; 
    } 
    return self; 
} 

在應用程序下一次調用是一個不同的方法在框架另一個類使用先前調用的結果:

Route53GetHostedZoneResponse *response = [[AmazonClientManager r53] getHostedZone:request]; 

完成此操作後,請求和響應都會釋放(如預期的那樣),第奇怪的是,當請求被釋放時,它也釋放zoneID。使用儀器我已經追蹤違規發佈:

[hostedZoneID release]; 

在Route53GetHostedZoneRequest.m的dealloc方法。

當返回到主控制器並釋放應用程序後,GetHostedZoneViewController被釋放時會導致殭屍。

如果我設置

detailViewController.zoneID = @"somestring"; 

的應用程序不會不管我多少次來回走崩潰。

任何人都可以解釋爲什麼這是崩潰,也許給我一些指導如何解決它?我真的不明白,爲什麼在了zoneid由[hostedZoneID發佈]發佈

+0

在我看來,你是不是在viewDidLoad中初始化你的區域ID。不清楚,因爲你沒有告訴我們它是什麼。 – 2013-07-15 22:24:11

+0

我相信它在主視圖控制器中的prepareForSegue中初始化,這似乎是接受的方式。 @Wain釘了這個問題,雖然......謝謝! – entr04y

回答

0

Route53GetHostedZoneRequest你應該有:

- (id)initWithHostedZoneID:(NSString *)theHostedZoneID 
{ 
    if (self = [self init]) { 
     self.hostedZoneID = theHostedZoneID; 
    } 
    return self; 
} 

因爲否則因爲這個代碼不使用ARC你是不是保留實例。


你的其他問題......

NSString實例是不可變的,所以當你指定它實際上並沒有被複制,它只是被保留的財產copy。所以zoneIDhostedZoneID實際上是相同的實例。

當您使用字符串文字時,它是一種特殊類型的對象,它並不真正被保留或釋放,因此您繞過了該問題。

+0

是的!我沒有意識到需要保留,因爲我認爲它是zoneID的副本。我仍然沒有真正知道爲什麼zoneID發佈時,hostedZoneID是... – entr04y

+0

以及爲什麼分配一個靜態字符串到zoneID阻止它崩潰... – entr04y

+0

瞭解它...謝謝! – entr04y

相關問題