0

だから私はアプリケーションに取り組んでおり、自動参照カウントを使用していないので、独自のメモリ管理を行う必要があります。最初のメソッドで UIView のレイヤーに値を設定するこのコードがあります。次に、2 番目の方法でそれを取得します。array1 は私の中で定義されていますviewDidLoad

これが私の最初の方法です

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
   CGFloat endLocation = [touch locationInView:self];
    CGRect rect = CGRectMake(startLocation.x, startLocation.y, endLocation.x, endLocation.y);
    UIView *rectstring = [[UIView alloc]initWithFrame:rect];
    NSString *string = [NSString stringWithFormat:@"%d",pencilbool];
    [[rectstring layer] setValue:string forKey:@"color"];
    NSString *string1 = [NSString stringWithFormat:@"%f",stepper.value];
    [[rectstring layer] setValue:string1 forKey:@"size234"];

        [array1 addObject:rectstring];
        [rectstring release];
        [string release];
        [string1 release];

}

2 番目の方法:

 -(IBAction)secondmethod {
        for (UIView *string1 in array1) {
            CGContextRef gc=UIGraphicGetCurrentContext();
            CGFloat width =[[string1.layer valueForKey:@"size234"] floatValue];
            CGContextSetLineWidth (gc, width);
            CGRect rect = [string1 frame];
            CGContextMoveToPoint(gc, rect.origin.x, rect.origin.y);
            CGContextAddLineToPoint(gc, rect.size.width, rect.size.height);
            CGContextStrokePath(gc);
        }
    }

これにより、 でエラーが発生しCGFloat width =[[string1.layer valueForKey:@"size234"] floatValue];ます。ただし、最初のメソッドでリリース呼び出しを削除すると:

 [string release];
 [string1 release];

それはうまくいきます。

なぜこれがエラーを引き起こしているのですか? 何か案は?

4

2 に答える 2

0

非 ARC ルールは、明示的に、または alloc や copy などを呼び出して、保持したもののみを解放することです。(より正確なドキュメントはhttps://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.htmlにあります) stringWithFormat:自動解放されたオブジェクトを返します。つまり、別のオブジェクトを追加するべきではありません。リリース。

于 2012-07-30T00:39:52.953 に答える
0

なぜこれがエラーを引き起こしているのですか?

文字列を作成するために呼び出したメソッド-stringWithFormat:は、自動解放されたオブジェクトを返すためです。alloc、retain、copy を呼び出さなかったので、release を呼び出すべきではありません。

于 2012-07-30T00:44:24.023 に答える