开发者

In a UIView dealloc method do you dealloc your properties BEFORE or AFTER [super dealloc]?

开发者 https://www.devze.com 2023-02-25 00:05 出处:网络
I have a custom UIView which gets loaded through a NIB inside a UIViewController. I\'ve been struggling with a -[UIScrollView retainCount]开发者_StackOverflow中文版: message sent to deallocated insta

I have a custom UIView which gets loaded through a NIB inside a UIViewController.

I've been struggling with a -[UIScrollView retainCount]开发者_StackOverflow中文版: message sent to deallocated instance error all day.

My custom UIView subclass dealloc method looked like this:

-(void)dealloc {
    [myScrollView dealloc];
    [someProperty dealloc];
    [super dealloc];
}

The problem was that it was always crashing on [super dealloc] because of the [myScrollView dealloc] preceding it.

When I changed the method around to:

-(void)dealloc {
   [super dealloc];
   [myScrollView dealloc];
   [someProperty dealloc];
}

Everything is working fine. My question is, does it make a difference if [super dealloc] is called first or last? In most examples I see it called last.


[super dealloc] should always be the last call in dealloc. Your problem is that you should be calling release on the other objects, not dealloc. dealloc is called by the runtime when the release count of the object reaches zero, your code should never call it directly.

Your code should therefore actually look like:

-(void)dealloc {
    [myScrollView release];
    [someProperty release];
    [super dealloc];
}
0

精彩评论

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

关注公众号