2011-02-05 68 views
0

這是我的代碼:試圖找出爲什麼我的應用程序崩潰?

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    NSLog(@"location for url1 B %@", locationForURL1); 
    if ((alertView.tag <= 3) && (alertView.tag >= 1)) { 
     if (buttonIndex == 1) { 
      NSLog(@"location for url1 %@", locationForURL1); 

的locationForURL1曾在它的代碼的其餘部分,直到這一點的條目,但它在第一次的NSLog這裏crahes。

所以我加了nszombieenabled並得到message sent to deallocated instance 0x508eda0。我如何使用它來找出我的問題?我聽說有人說把它放在init方法中,這讓我困惑,因爲我找不到init方法。我從來沒有做過這樣的調試。

編輯:

我是這樣分配的:

@interface RewriteViewController : UIViewController <MPMediaPickerControllerDelegate> { 

    NSString *locationForURL1; 
} 

@property (nonatomic,retain) NSString *locationForURL1; 

我覺得這件事情做的self.VARIABLE的事情,但我永遠無法弄清楚當我打算把自己。如果我打算換一些別的東西。

這是我必須locationForURL1引用的.m文件:

@synthesize locationForURL1; 

-(void)getWeatherLocation { 

if (currentResult == 1) { 
     self.locationForURL1 = locationTown; 
     locationForURL1 = [locationForURL1 stringByAppendingString:@","]; 
     locationForURL1 = [locationForURL1 stringByAppendingString:locationCountry]; 

    } 
} 


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 
    if ((alertView.tag <= 3) && (alertView.tag >= 1)) { 
     if (buttonIndex == 1) { 
      NSLog(@"location for url1 %@", self.locationForURL1); 
     self.weatherFullURL = [self.weatherFullURL stringByAppendingString:self.locationForURL1]; 

     } 
    } 
} 

-(void)dealloc { 


    [locationForURL1 release]; 

[super dealloc]; 

} 
+0

你如何分配`locationForURL1`? – aqua 2011-02-05 02:46:40

回答

3
self.locationForURL1 = locationTown; 
    locationForURL1 = [locationForURL1 stringByAppendingString:@","]; 
    locationForURL1 = [locationForURL1 stringByAppendingString:locationCountry]; 

您可以利用self.locationForURL1,然後立即覆蓋有兩個自動釋放對象分配保留locationTown。所以,你泄露一個對象,然後當自動釋放池收穫stringByAppendingString:的結果時發生崩潰。

1

你一定不能保留locationForURL1在任何你正在創建它。我建議將其添加爲一個屬性類:

@interface YourClass : UIViewController { 
    NSString *locationForURL1; 
} 

@property (nonatomic, copy) NSString *locationForURL1; 

然後在您的viewDidLoad(或任何你正在創建一個字符串,其中),這樣做:

NSString *location = [[NSString alloc] initWith....]; 
self.locationForURL1 = location; 
[location release]; 

然後在你的-alertView :clickedButtonAtIndex:方法,只是將其稱爲self.locationForURL1,你應該沒問題。

+0

我不明白,第二點。爲什麼我不只是把self.locationForUrl1 = @「我想要什麼」;? – Andrew 2011-02-05 03:08:22

+0

是的,你也可以這樣做,我只是假設字符串更復雜。你可以把self.locationForURL1 = @「任何你想要的」,它會工作得很好。 – 2011-02-05 03:11:10

相關問題