0

I have a property in class mainView declared thusly:

@property (nonatomic, retain) FriendsView *friendsView;

For some reason, it's not being dealloc'ed when mainView is. *Unless* I do self.friendsView = nil in mainView's dealloc method.

Very odd. But wait, it gets odder..

When I run it in instruments, it dealloc's properly, 100% of the time.
When I dun it normally, with breakpoints and NSLog's in friendsView's dealloc method, they are never hit.

Any ideas as to what could be going on?

Edit: I should've said, I'm using ARC. Hence my confusion.

Max
  • 2,760
  • 1
  • 28
  • 47
  • @JacquesCousteau Nothing else should be retaining it at this point. As is reinforced by it being successfully released when I set the property to nil. But the fact that I have to set it to nil suggests something is wrong. – Max May 14 '12 at 17:48
  • Updated the question to clarify I'm using ARC. – Max May 14 '12 at 17:56

1 Answers1

1

If you set that property anywhere then you are incrementing the retain count, and it should not be released when mainView is and you should be releasing it dealloc of mainView. That's expected behavior, not strange. When you have a retain or copy object property you should do on init:

friendsView = nil;

And on dealloc:

[friendsView release]; //(or self.friendsView = nil which accomplishes the same)

Properties do not behave like an object that you add to a view where they will be released when the parent view is. They will survive once the parent object is released if their retain count is > 0.

Joel
  • 15,654
  • 5
  • 37
  • 60
  • Don't use `self.friendsView = nil` in `-dealloc`. Although it may appear correct, if the class is subclassed and `-setFriendsView:` is overridden, weird things can happen. – Jonathan Grynspan May 14 '12 at 17:43
  • I agree. I was pointing out that his self.friendsView = nil was accomplishing the same thing in that case where he was doing that. But agree it should be as I wrote it. – Joel May 14 '12 at 17:44
  • 1
    It's not necessary to initialize properties to nil; Objective-C objects are created with zero-initialized members. – Seamus Campbell May 14 '12 at 17:46
  • Sorry, I forgot to mention in my original question, I'm using ARC. Just to note, I haven't overridden `setFriendsView:`. – Max May 14 '12 at 17:49