2012-03-13 8 views
0

私はこの使用してNSDisctionaryでキーの値を設定しています:私はcurrentColorの値にEXC悪いアクセスアプリのクラッシュを(ログインしようとEXC悪いアクセスエラー

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 

NSLog(@"touchesEnded currentcolor: %@", currentColor); 

if (currentTool == @"paint") { 
NSLog(@"End Paint"); 
CGPoint point = [[touches anyObject] locationInView:self.view]; 
MaskPath * myNewPath = [mapView.drawthis containsPoint:point]; 
[myNewPath.attributes setValue:currentColor forKey:@"fill"]; 
[mapView setNeedsDisplay]; 

} else { 
NSLog(@"End Pattern"); 

currentColor = @"1.png"; 

CGPoint point = [[touches anyObject] locationInView:self.view]; 
MaskPath * myNewPath = [mapView.drawthis containsPoint:point]; 
[myNewPath.attributes setValue:currentColor forKey:@"fill"]; 
[mapView setNeedsDisplay]; 
} 
} 

3行目) ログアウトしてハードコードされた値を使用すると、すべて正常に動作します。また、ifステートメントの最初の部分でうまく動作します。私はcurrentColorを割り当てる関数をチェックし、正しい値を渡しています。その時点でcurrentColorの値をハードコードするとうまく動作します。私は、アナライザーを実行していると私はメモリリークや問題はありません。この問題を他にどのように追跡できますか?

+0

あなたが宣言され、色を割り当てコードを表示することができますか? –

+0

'NSLog'でクラッシュした場合は、' NSDictionary'とはまったく関係ありません。どこにcurrentColorを設定しますか?私はあなたがそれを正しく保持していないだろうと確信しています:) – deanWombourne

+0

これは、 'currentColor'が割り当て解除されたためです。 'currentColor'を宣言したコードと' touchesEnded:withEvent: 'メソッドの外でそれをどこに設定するのかを指定します。 – Jeremy

答えて

1

あなたの例に示されているように、autorelease変数を使って作業しているというのが目立つ問題です。ほとんどの場合、あなたはその方法の外で同じことをやっているでしょう。

私はあなたがcurrentColor財産を作るお勧めします。

@property (nonatomic, retain) NSString *currentColor; 

次に、それを設定するために、あなたはこのようなものだろう:あなたが保持してcurrentColorプロパティを宣言しているので

self.currentColor = @"SomeColor"; 

を、あなたは

あなたのNSLogに渡すと悪いアクセスを得ることはありませんすでにcurrentColorプロパティを持っている、あなたは単に、あなたがやるべきこと、IVARを割り当てる:

currentColor = [NSString alloc] initWithString:@"Foo"]; 

それを要約すると:

1. self.currentColor = @"Foo"; //<-This is automagically retained 
2. currentColor = [NSString alloc] initWithString:@"Foo"]; //<-Accessing the ivar directly, so you must ownify the string :D 
関連する問題