iphone:哪个函数/方法调用增加了retainCounter?为什么内存管理表现得如此奇怪?

发布于 2024-11-08 11:17:36 字数 1233 浏览 2 评论 0原文

嘿人们, 我对 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 技术交流群。

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

发布评论

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

评论(2

墨小墨 2024-11-15 11:17:36

不要使用保留计数作为调试辅助。如果不知道系统和第三方框架是如何实现的,您就无法知道保留计数应该是多少。

非常简单:

  1. 如果您分配复制保留一个对象,那么您“拥有”它并且需要释放 code> 或稍后自动释放它,
  2. 否则您拥有它,并且您不对它的内存负责

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:

  1. If you alloc or copy or retain an object, you "own" it and need to either release or autorelease it at some later point
  2. Otherwise you do not own it and you are not responsible for its memory
烧了回忆取暖 2024-11-15 11:17:36

正如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.

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