开发者

Using self dot notation doesn't call dealloc

开发者 https://www.devze.com 2023-02-20 15:49 出处:网络
The dealloc method of my view controller class is not called when popped after being pushed with the following code:

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;


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]

0

精彩评论

暂无评论...
验证码 换一张
取 消