当我释放分配和启动的 NSMutableArray 时,我的代码中断
使用此代码,我想在用户点击的位置添加图像。我想为每个水龙头添加一个新的。
-(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 testPointArray 没有分配给属性,它是一个普通的 ivar。执行该行
会泄漏 testPointArray 中先前的内容。将
testPointArray
声明为保留属性并更改为。然后保留
[tempTestPointArray release];
编辑:
所以这段代码失败的原因与属性的魔力有关。以下代码是等效的。
当您执行 @sythesize testPointArray; 时,它会生成与此类似的 setter 方法:
因此,当您不使用属性符号
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 lineIs leaking whatever is previously in
testPointArray
. DeclaretestPointArray
as a retained property and change to.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.
When you do the
@sythesize testPointArray;
it is generating a setter method similar to this: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