dealloc implemented in a category
ID |
objectivec.correctness.dealloc_in_category |
Severity |
low |
Remediation Complexity |
medium |
Remediation Risk |
medium |
Remediation Effort |
medium |
Resource |
Api Misuse |
Language |
Objective-C |
Description
A category must not implement dealloc. A category method replaces the class’s own implementation rather than extending it, with no way to reach the replaced version, so the class’s real teardown is silently dropped - and if two categories on the same class both define it, which one wins is undefined. Move the teardown into the class’s own @implementation, or have the category observe the object’s lifetime instead.
Rationale
A category must not implement dealloc. A category method replaces the class’s own implementation rather than extending it, with no way to reach the replaced version, so the class’s real teardown is silently dropped - and if two categories on the same class both define it, which one wins is undefined. Move the teardown into the class’s own @implementation, or have the category observe the object’s lifetime instead.
The following code illustrates the pattern detected by this rule:
// FLAGGED: dealloc implemented in a category
@implementation Article (Networking)
- (void)fetchRemoteBody {
NSLog(@"fetching");
}
- (void)dealloc {
[_connection cancel];
[super dealloc];
}
@end