当我释放分配和启动的 NSMutableArray 时,我的代码中断

发布于 2024-12-11 02:56:03 字数 1793 浏览 0 评论 0原文

使用此代码,我想在用户点击的位置添加图像。我想为每个水龙头添加一个新的。

-(void) foundDoubleTap:(UITapGestureRecognizer *) recognizer
{       
    UIView *piece = recognizer.view;
    CGPoint locationInView = [recognizer locationInView:piece];

    UIImageView *testPoint = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"inner-circle.png"]];
    testPoint.frame = CGRectMake(0, 0, 20, 20);
    testPoint.center = CGPointMake(locationInView.x, locationInView.y);
    [self.imageView addSubview:testPoint];

    NSMutableArray *tempTestPointArray = [[NSMutableArray alloc] initWithArray:testPointArray];
    [tempTestPointArray addObject:testPoint];
    testPointArray = tempTestPointArray;

    NSLog(@"testPointArray: %@", testPointArray);

    CGRect myRect = CGRectMake((testPoint.center.x + 12), (testPoint.center.y + 12), 10, 10);

    UILabel *myLabel = [[UILabel alloc] initWithFrame:myRect];
    myLabel.text = [NSString stringWithFormat:@"Point %d", [testPointArray count]];
    myLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:10];
    [myLabel sizeToFit];
    [imageView addSubview:myLabel];
    [myLabel release];

    [testPoint release];
    //[tempTestPointArray release];
}

为什么当我释放 tempTestPointArray 时,当我实现第二次点击时我的代码会中断?它崩溃于:

NSMutableArray *tempTestPointArray = [[NSMutableArray alloc] initWithArray:testPointArray];

当我注释掉它的版本时,分析器不会将其标记为泄漏。规则发生了什么,如果你分配/初始化它,你必须释放它?

编辑:添加 .h 文件

.h 文件:

@interface TestPointMapViewController : UIViewController <UIScrollViewDelegate, UITextFieldDelegate>
{
    //other code
    NSArray *testPointArray;
}
//other code
@property (nonatomic, retain) NSArray *testPointArray;
//other code
@end

然后在 .m 文件中添加@synthesize testPointArray。

with this code, i want to add an image at the place the user taps. i want to add a new one for each tap.

-(void) foundDoubleTap:(UITapGestureRecognizer *) recognizer
{       
    UIView *piece = recognizer.view;
    CGPoint locationInView = [recognizer locationInView:piece];

    UIImageView *testPoint = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"inner-circle.png"]];
    testPoint.frame = CGRectMake(0, 0, 20, 20);
    testPoint.center = CGPointMake(locationInView.x, locationInView.y);
    [self.imageView addSubview:testPoint];

    NSMutableArray *tempTestPointArray = [[NSMutableArray alloc] initWithArray:testPointArray];
    [tempTestPointArray addObject:testPoint];
    testPointArray = tempTestPointArray;

    NSLog(@"testPointArray: %@", testPointArray);

    CGRect myRect = CGRectMake((testPoint.center.x + 12), (testPoint.center.y + 12), 10, 10);

    UILabel *myLabel = [[UILabel alloc] initWithFrame:myRect];
    myLabel.text = [NSString stringWithFormat:@"Point %d", [testPointArray count]];
    myLabel.font = [UIFont fontWithName:@"Trebuchet MS" size:10];
    [myLabel sizeToFit];
    [imageView addSubview:myLabel];
    [myLabel release];

    [testPoint release];
    //[tempTestPointArray release];
}

why is it that when i release tempTestPointArray, my code breaks when i implement a second tap? it crashes on:

NSMutableArray *tempTestPointArray = [[NSMutableArray alloc] initWithArray:testPointArray];

when i comment out the release for it, the Analyzer does not flag it as a leak. what happened to the rule, if you alloc/init it, you have to release it?

EDIT: adding .h file

.h file:

@interface TestPointMapViewController : UIViewController <UIScrollViewDelegate, UITextFieldDelegate>
{
    //other code
    NSArray *testPointArray;
}
//other code
@property (nonatomic, retain) NSArray *testPointArray;
//other code
@end

and then @synthesize testPointArray in .m file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情深如许 2024-12-18 02:56:03

您的 testPointArray 没有分配给属性,它是一个普通的 ivar。执行该行

testPointArray = tempTestPointArray;

会泄漏 testPointArray 中先前的内容。将 testPointArray 声明为保留属性并更改为。

self.testPointArray = tempTestPointArray;

然后保留 [tempTestPointArray release];

编辑:

所以这段代码失败的原因与属性的魔力有关。以下代码是等效的。

self.testPointArray = tempTestPointArray;
[self setTestPointArray:tempTestPointArray];

当您执行 @sythesize testPointArray; 时,它会生成与此类似的 setter 方法:

- (void)setTestPointArray:(NSMutableArray *)array {
    id temp = testPointArray;
    testPointArray = [array retain];
    [temp release];
}

因此,当您不使用属性符号 self.testPointArray 时,您不会保留变量正确。您还会丢失对保留对象的引用,该对象现在是泄漏。

这有道理吗?如果没有,请查看此内容。

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

Your testPointArray is not assigning to a property, it is a plain ivar. Doing the line

testPointArray = tempTestPointArray;

Is leaking whatever is previously in testPointArray. Declare testPointArray as a retained property and change to.

self.testPointArray = tempTestPointArray;

Then keep the [tempTestPointArray release];

EDIT:

So the reason why this code is failing has to do with the magic of properties. The following code is equivalent.

self.testPointArray = tempTestPointArray;
[self setTestPointArray:tempTestPointArray];

When you do the @sythesize testPointArray; it is generating a setter method similar to this:

- (void)setTestPointArray:(NSMutableArray *)array {
    id temp = testPointArray;
    testPointArray = [array retain];
    [temp release];
}

So when you don't use the property notation self.testPointArray you are not retaining the variable correctly. You are also losing a reference to a retained object, which is now a leak.

That make sense? If not please review this.

http://developer.apple.com/library/mac/#documentation/cocoa/conceptual/objectiveC/Chapters/ocProperties.html#//apple_ref/doc/uid/TP30001163-CH17-SW1

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文