When testing the application that I am developing, I came across this problem, which I would like to discuss. I have a class that should receive a message from the server and must pass the message to the view. Here is how I do it:
- (void) onMessage:(DFTopicMessage *) message {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"serverMessage"
object:message];
}
The class does nothing with the message. When I profile using tools → Leaks, this line of code is marked as a potential leak. I understand that the problem is that the message is highlighted, used, and never released. The first thing that is strange is that I use ARC in my project and therefore expect the OS to release var automatically, but this is obviously not the case (then why doesn't it release var?). In any case, I started thinking how to avoid this leak. Just setting the message to nil, for example in:
- (void) onMessage:(DFTopicMessage *) message {
[[NSNotificationCenter defaultCenter]
postNotificationName:@"serverMessage"
object:message];
message = nil;
}
. ivar , :
@interface myClass()
@property(nonatomic) DFTopicMessage *message;
@end
@implementation myClass {
@synthetize message;
....
- (void) onMessage:(DFTopicMessage *) msg {
[self setMessage:msg];
[[NSNotificationCenter defaultCenter]
postNotificationName:@"serverMessage"
object:[self message]];
}
}
, , → . : , var ARC?
!