我正在使用带参数的字典发送到Web服务。我不断从它那里得到内存泄漏,但是我为该对象做了一个发布。我的代码:
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *clientID = [prefs stringForKey:@"ClientID"];
//Call web service
id delegate1 = self;
AsyncWebServiceController * webService = [[[AsyncWebServiceController alloc] initWithDelegate:delegate1
selSucceeded:@selector(webServiceConnectionSucceeded:)
selFailed:@selector(webServiceconnectionFailed:)] autorelease];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; //Memory leak here
[params setValue:clientID forKey:@"clientId"]; //Memory leak here
[params setValue:[self.jobDetails objectForKey:@"BoardId"] forKey:@"jobBoardId"];
[params setValue:[self.jobDetails objectForKey:@"Id"] forKey:@"jobId"];
[webService startRequest:@"JobReviewGet" parameters:params];
[params release];
有什么想法吗?谢谢!
答案 0 :(得分:2)
您应该使用:
- (void)setObject:(id)anObject forKey:(id)aKey;
因此,您的代码应如下所示:“
NSMutableDictionary *params =
[[NSMutableDictionary alloc] init]; //Memory leak here
[params setObject:clientID forKey:@"clientId"]; //Memory leak here
[params setObject:[self.jobDetails objectForKey:@"BoardId"] forKey:@"jobBoardId"];
[params setObject:[self.jobDetails objectForKey:@"Id"] forKey:@"jobId"];
[webService startRequest:@"JobReviewGet" parameters:params];
[params release];
[webService startRequest:@"JobReviewGet" parameters:params];
方法对param做了什么,是否保留了它?如果是这样,你还需要在那里发布它。
还有一点你需要释放 webService
。
答案 1 :(得分:2)
用分析构建。这可能会提供一些指示。
除此之外:
答案 2 :(得分:2)
为什么不使用便利构造函数?
NSMutableDictionary *params = [NSMutableDictionary dictionary];
然后就没有必要释放它了。这应该可以解决你的问题。
答案 3 :(得分:1)
您是否在某个时候发布了您的webService对象? AsyncWebServiceController的委托是指定的还是保留的属性?通常应为代理分配属性以避免保留循环。