Objective-C 内存管理:设置保留属性时会发生什么?

发布于 2024-12-01 10:14:50 字数 433 浏览 1 评论 0原文

我想了解以下代码行的内存管理含义:

// in tableView:cellForRowAtIndexPath
cell.accessoryView = [[UIImageView alloc] initWithImage:
                      [UIImage imageNamed:@"test.png"];

我正在调用 alloc 这通常意味着我在某处调用 releaseUITableViewCellaccessoryView setter 属性是 retain 所以(我认为)单元格将“取得 UIImageView 的所有权” 。 上面这行代码在内存管理方面到底发生了什么?

I'd like to understand the memory management implications of the following line of code:

// in tableView:cellForRowAtIndexPath
cell.accessoryView = [[UIImageView alloc] initWithImage:
                      [UIImage imageNamed:@"test.png"];

I'm calling alloc which usually means I call release somewhere. The UITableViewCell's accessoryView setter property is retain so (I think) the cell will "take ownership" of the of the UIImageView. What exactly is happening in the above line of code in regards to memory management?

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

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

发布评论

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

评论(3

笨笨の傻瓜 2024-12-08 10:14:51

第一:+alloc 保留 UIImageView(或者,“您从 UIImageView 的所有权开始”)

第二:+imageNamed 自动释放 UIImage(或者,“+imageNamed 不给您 UIImage 的所有权”)

第三: AccessoriesView 保留 UIImageView (或者,“附件视图取得 UIImageView 的所有权”)

由于您现在有 UIImageView 的两个所有者,这可能是泄漏,除非您故意保留它以便以后使用,并进行相应的管理。

First: +alloc retains the UIImageView (or, alternately, "you start with ownership of the UIImageView")

Second: +imageNamed autoreleases the UIImage (or, "+imageNamed does not give you ownership of the UIImage")

Third: the setter for accessoryView retains the UIImageView (or, "the accessory view takes ownership of the UIImageView")

Since you now have two owners for the UIImageView, that's probably a leak unless you're intentionally keeping it around to use later, and managing it accordingly.

瞎闹 2024-12-08 10:14:51

可以这样想:您正在调用 alloc/init,因此您拥有它。当您不想再拥有它时,您必须释放它。

您可以假设 cell.accessoryView 拥有所有权,除非文档另有说明(如委托),因此一旦将其分配给 cell.accessoryView,您可能不再需要拥有它。你应该释放它。

总之,该行至少保留两次:一次与 alloc/init 一起使用,至少一次与 cell.accessoryView 的分配一起使用。您只负责一个版本,即 alloc/init 版本。

Think of it this way: you're calling alloc/init, so you own it. You must release it when you no longer want to own it.

You can assume that cell.accessoryView takes ownership unless the docs say otherwise (like with delegates), so once you assign it to cell.accessoryView, you probably don't need to own it anymore. You should release it.

In summary, that line is retaining at least twice: once with the alloc/init and at least once with the assignment to cell.accessoryView. You are only responsible for one release, the one for alloc/init.

世界等同你 2024-12-08 10:14:50

如果你不在某个地方释放视图,那么它就会被泄露。所以你可能想做

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"];
cell.accessoryView = imageView;
[imageView release];

或者你也可以做

cell.accessoryView = [[[UIImageView alloc] initWithImage:
                  [UIImage imageNamed:@"test.png"] autorelease];

If you don't release the view somewhere then it will be leaked. So you might want to do

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"test.png"];
cell.accessoryView = imageView;
[imageView release];

or you could also do

cell.accessoryView = [[[UIImageView alloc] initWithImage:
                  [UIImage imageNamed:@"test.png"] autorelease];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文