objective-c release
I have this class in DNS.h
@interface DNS : NSObject <NSXMLParserDelegate, NSNetServiceDelegate> {
NSMutableArray *servsPublished;
}
@property(nonatomic, retain) NSMutableArray *servsPublished;
Then in the implementation file DNS.m there's a method where I release it and I get the exec bad access memory error. This is method, it conforms to the NSXMLParserDelegate protocol
-(void) parserDidEndDocument:(NSXMLParser *)parser {
NSNetService *client;
开发者_Go百科 for (NSDictionary *aService in servToPublish) {
client = [[NSNetService alloc] initWithDomain:@"local"
type:[aService objectForKey:@"serviceType"]
name:[aService objectForKey:@"name"]
port: [(NSNumber *)[aService objectForKey:@"port"] intValue]];
[client setDelegate: self];
[client publish];
//release this service and the client
[aService release];
//[client release];
}
//free the array of Dictionary containing the services
[servToPublish release];
}
Maybe the first thought is that is nil, but actually I use it inside the method checking whether is nil or not, and then free it.
Does It have something to do with the retain property? THX.
Do NOT release aService in the for loop. You have not created them here (no alloc/new/copy) so you should not release them. When you release servToPublish all content will automatically be released, but if you release them in your loop you will cause too many release messages to be sent.
It's correct to release client
in this case as you do allocate it.
First, run build and analyze. The static analyzer is pretty good at pointing out certain types of memory management problems.
Second, unless you're in dealloc, you probably don't want to call release directly on servsPublished. Instead, use self.servsPublished = nil
. That will properly release the object and set the corresponding ivar to nil.
精彩评论