-
Notifications
You must be signed in to change notification settings - Fork 6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Set the ivar to nil after release it in dealloc method. #38737
Conversation
It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption to this rule, contact Hixie on the #hackers channel in Chat (don't just cc him here, he won't see it! He's on Discord!). If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. |
Hi, @cyanglaz. I have some difficulties writing the test. Is there any way to retain the ivar in the outside of |
Yes there are hacks to do that in objective-C. https://medium.com/@AdamRoberts/accessing-private-properties-and-private-ivars-in-objective-c-from-another-class-bc59edcc36cb |
@cyanglaz I'm surprised this is necessary per your comment in #37666 (comment), it's being dealloc'd so none of these ivars should ever be used after this point. Even in MRC code I've worked in before we didn't nil out ivars in dealloc... |
it's been a while since I wrote MRC code, but iirc you only need to nil out things like delegates to prevent dangling pointers:
You don't need to nil out |
It is to prevent tricky dangling pointers. Examples like: - (void) dealloc {
[_propertyA release];
[self aCleanUpMethodDoesSomethingWithPropertyA];
[super dealloc];
} or a pointer pointing to this variable outside of the class. Even for ivars that only defined in .mm files, there is no guarantee that they won't be exposed in a public getter later. When that happens, it is easy for the author to forget to nil out the ivar in dealloc. |
That's the pattern that justifies "never call |
Not sure I follow. How would you access these ivars through public getter after calling dealloc? Also as @jmagman pointed out, calling methods on self within dealloc goes against google objc guidelines. I had a brief look through |
I would think that accessing ivar after releasing it is generally a bug, and setting it to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This almost always indicates there is some other issue. If the instance owning the variable has been dealloc-ed, then someone managed to get a reference to that variable with incorrectly retaining it or reasoning about its lifecycle.
Let's close this, and stop using this |
As #37666 (comment) mentioned, the correct pattern is the set the ivar to nil after release it in dealloc method.
Related issue: flutter/flutter#118217
Pre-launch Checklist
///
).