使用自点符号不会调用 dealloc

发布于 2024-10-27 18:29:50 字数 866 浏览 2 评论 0原文

使用以下代码推送后弹出时,不会调用我的视图控制器类的 dealloc 方法:

    self.playerViewController = [[VideoPlayerViewController alloc] init];

[self.playerViewController set_video:video];

    [self.navigationController pushViewController:self.playerViewController animated:YES];

[self.playerViewController release];

但是,如果我将推送代码更改为以下内容,则在弹出视图控制器后会适当地调用我的 dealloc:

    playerViewController = [[VideoPlayerViewController alloc] init];

[playerViewController set_video:video];

[self.navigationController pushViewController:playerViewController animated:YES];

[playerViewController release];

我想我理解了使用点符号/self,但显然不是。谁能解释一下这里的问题吗?

这是属性:

@property (nonatomic, retain) VideoPlayerViewController *playerViewController;

这是合成:

@synthesize playerViewController;

The dealloc method of my view controller class is not called when popped after being pushed with the following code:

    self.playerViewController = [[VideoPlayerViewController alloc] init];

[self.playerViewController set_video:video];

    [self.navigationController pushViewController:self.playerViewController animated:YES];

[self.playerViewController release];

However if I change the push code to the following, then my dealloc is called appropriately after the view controller is popped:

    playerViewController = [[VideoPlayerViewController alloc] init];

[playerViewController set_video:video];

[self.navigationController pushViewController:playerViewController animated:YES];

[playerViewController release];

I thought I understood the use of dot notation/self, but obviously not. Can anyone explain the problem here?

Here is the property:

@property (nonatomic, retain) VideoPlayerViewController *playerViewController;

and here is the synthesize:

@synthesize playerViewController;

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

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

发布评论

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

评论(1

疧_╮線 2024-11-03 18:29:50

你保留了两次。

self.playerViewController = [[VideoPlayerViewController alloc] init];
    ^ retain + 1                                        ^^^^^ retain + 1

但你只释放一次。

要解决内存管理问题,您可以将代码更改为如下所示:

self.playerViewController = [[[VideoPlayerViewController alloc] init] autorelease];

许多人说 [self.foo release] 是不好的风格。您应该考虑将其替换为 [foo release]

You are retaining twice.

self.playerViewController = [[VideoPlayerViewController alloc] init];
    ^ retain + 1                                        ^^^^^ retain + 1

But you only release one time.

To fix your memory management issue you could change the code to something like this:

self.playerViewController = [[[VideoPlayerViewController alloc] init] autorelease];

And many people say [self.foo release] is bad style. You should consider to replace it with [foo release]

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