2011-02-05 4 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メソッドを見つけることができないので、混乱させます。私は前にこのようなデバッグをしたことはありません。

EDIT:

私はこのようにそれを割り当てています:

@interface RewriteViewController : UIViewController <MPMediaPickerControllerDelegate> { 

    NSString *locationForURL1; 
} 

@property (nonatomic,retain) NSString *locationForURL1; 

私はそれがself.VARIABLE事とは何かだと思うが、私は置くことを意図していたとき、私は把握ことはできません自己。もし私が別のものを置くつもりならば。

これは.MファイルにlocationForURL1に私が持っているすべての参照です:

@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

答えて

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

あなたは、すぐに2つの自動解放オブジェクトとその割り当てを上書きし、self.locationForURL1を使用してlocationTownを保持しています。だから、あなたは1つのオブジェクトを漏らしているし、自動解放プールがstringByAppendingString:の結果を得たときにあなたのクラッシュが発生します。

+0

だから私は自分でそれらの行を開始する必要があります。あまりにも? – Andrew

+0

ちょうどそれを変更しました。できます!どうもありがとう! =) – Andrew

+1

.NSStringプロパティにretainの代わりにcopyを使うことも考えてください。 NSMutableStringもNSStringですので、保持のみを使用すると、オブジェクトのプロパティを設定し、他のコードが変更されたため、あなたが知らないうちにプロパティを変更することができます。 retainの代わりにcopyを使用すると、それはまだ保持されていますが、不必要な変更からユーザーを保護するために文字列の不変なコピーを作成しています。 –

1

あなたは今まであなたがそれを作成しているlocationForURL1を保持してはいけません。 、

@interface YourClass : UIViewController { 
    NSString *locationForURL1; 
} 

@property (nonatomic, copy) NSString *locationForURL1; 
あなたのviewDidLoadで次に

(あるいはあなたがその文字列を作成しているこれまでの)ような何か:私はあなたのクラスにプロパティとして追加することをお勧めあなたの-alertViewに続いて

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

を:clickedButtonAtIndex:メソッド、ちょうどそれをself.locationForURL1として参照してください。

+0

私はその2番目のビットを理解していません。なぜ私はself.locationForUrl1 = @ "何が欲しいの?" – Andrew

+0

はい、あなたもそれを行うことができます、私はちょうどその文字列がより複雑であると仮定しました。あなたは単にself.locationForURL1 = @ "あなたが望むものは何でも"置くことができ、うまくいくでしょう。 –

+0

ちょうど私のコードをチェックして、それは私が思っているものです。私は主なビットを編集してあなたを見せます。 – Andrew

関連する問題