iphone:哪个函数/方法调用增加了retainCounter?为什么内存管理表现得如此奇怪?
嘿人们, 我对 iPhone 内存管理的理解有问题。
我遇到以下奇怪的情况:
CAGradientLayer * hButtonLayer = [[CAGradientLayer alloc] init];
NSLog(@"1: retaincounter is?: %d", [hButtonLayer retainCount]);
[hButtonLayer setBounds:tempButton.bounds];
NSLog(@"2: retaincounter is?: %d", [hButtonLayer retainCount]);
[hButtonLayer setColors:[NSArray arrayWithObjects:
[UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0],
[UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0], nil]];
NSLog(@"3: retaincounter is?: %d", [hButtonLayer retainCount]);
[[tempButton layer] insertSublayer:hButtonLayer atIndex:0];
NSLog(@"4: retaincounter is?: %d", [hButtonLayer retainCount]);
控制台上的输出显示以下内容:
1: retaincounter is?: 1
2: retaincounter is?: 2
3: retaincounter is?: 2
4: retaincounter is?: 3
好的,在 1)很明显计数器等于 1,因为层已分配并初始化。但为什么在 2) 中“setBounds”方法会增加保留计数器?并且在3)保留计数器没有通过“setColors”方法增加...并且“insertSublayer”再次增加保留计数器!
为什么这些方法会增加计数器?我怎么知道某些框架方法是否增加了某些内容?我的意思是,如果我减少保留计数器并且某些框架方法没有增加计数器,我会崩溃 - 我应该如何确定方法是否增加保留计数器?
感谢您的帮助!
hey people,
I have a problem with my understanding of the memory management in the iphone.
I have the following strange situation:
CAGradientLayer * hButtonLayer = [[CAGradientLayer alloc] init];
NSLog(@"1: retaincounter is?: %d", [hButtonLayer retainCount]);
[hButtonLayer setBounds:tempButton.bounds];
NSLog(@"2: retaincounter is?: %d", [hButtonLayer retainCount]);
[hButtonLayer setColors:[NSArray arrayWithObjects:
[UIColor colorWithRed:0.2 green:0.3 blue:0.4 alpha:1.0],
[UIColor colorWithRed:0.4 green:0.5 blue:0.6 alpha:1.0], nil]];
NSLog(@"3: retaincounter is?: %d", [hButtonLayer retainCount]);
[[tempButton layer] insertSublayer:hButtonLayer atIndex:0];
NSLog(@"4: retaincounter is?: %d", [hButtonLayer retainCount]);
the output on the console shows the following:
1: retaincounter is?: 1
2: retaincounter is?: 2
3: retaincounter is?: 2
4: retaincounter is?: 3
ok, at 1) it's clear that the counter equals 1, because the Layer is alloced and initialized. but why is at 2) the "setBounds"-methid increasing the retain counter? and at 3) the retain counter is not increased by the "setColors"-method... and "insertSublayer" is again increasing the retain counter!
why are these methods increasing the counter? how should I know, if some framework-method increases something? I mean, if I decreases the retain counter and some framework-method didn't increase the counter, I will get a crash - how should I determine, if a method increases or not increases the retain counter?
thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要使用保留计数作为调试辅助。如果不知道系统和第三方框架是如何实现的,您就无法知道保留计数应该是多少。
非常简单:
分配
或复制
或保留
一个对象,那么您“拥有”它并且需要释放
code> 或稍后自动释放它,Don't use the retain count as a debugging aid. Without knowing how system and third party frameworks are implemented you cannot know what the retain count should be.
It's very simple:
alloc
orcopy
orretain
an object, you "own" it and need to eitherrelease
orautorelease
it at some later point正如Stephen所说,任何像NSArray、NSDictionary这样的容器都会保留对象。
UIView的子视图是通过数组维护的,因此任何添加的子视图都会被保留。
对象的属性、内存管理策略可以在Apple的文档中找到。或者,您可以转到类的定义并阅读其声明。
As Stephen said, and any container like NSArray, NSDictionary will retain object.
The sub views of UIView is maintained by array, so any added sub view will be retained.
The properties of an object, the memory management policy can be found on Apple's document. Or, you can go to the definition of the class and read its declaration.