次のコードを使用すると、リークがあると言われました。
- (void)dealloc
{
[connection release], connection = nil;
[responseData release],responseData = nil;
[cityCode release], cityCode = nil;
[requestUrlString release], requestUrlString = nil;
[returnDataDic release], returnDataDic = nil;
[super dealloc];
}
- (id)initWithCityCode:(NSString *)aCityCode
requestURL:(NSString*)urlString
responseType:(SWEngineRequestType)theResponsetype
target:(id)theTarget
action:(SEL)theAction
{
if ((self = [super init]))
{
_isExecuting = NO;
_isFinished = NO;
target = theTarget;
action = theAction;
cityCode = [aCityCode retain];
requestUrlString = [urlString copy];
responseType = theResponsetype;
returnDataDic = [[NSMutableDictionary alloc] initWithCapacity:1];
if (cityCode)
{
[returnDataDic setObject:cityCode forKey:SWEATHER_CITYCODE];
}
[returnDataDic setObject:[NSNumber numberWithInt:responseType] forKey:SWEATHER_DOWNTYPE];
}
return self;
}
- (BOOL)isConcurrent
{
return YES;
}
- (void)finish
{
[self willChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
_isExecuting = NO;
_isFinished = YES;
[self didChangeValueForKey:@"isExecuting"];
[self didChangeValueForKey:@"isFinished"];
[connection release], connection = nil;
[responseData release],responseData = nil;
[cityCode release], cityCode = nil;
[requestUrlString release], requestUrlString = nil;
[returnDataDic release], returnDataDic = nil;
done = YES;
}
- (BOOL)isExecuting
{
return _isExecuting;
}
- (BOOL)isFinished
{
return _isFinished;
}
- (void)main
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
done = NO;
if ([self isCancelled])
{
[self willChangeValueForKey:@"isFinished"];
_isFinished = YES;
[self didChangeValueForKey:@"isFinished"];
[pool release];
return;
}
[self willChangeValueForKey:@"isExecuting"];
_isExecuting = YES;
[self didChangeValueForKey:@"isExecuting"];
NSURL * urlToDownLoad = [NSURL URLWithString:[requestUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSURLRequest *request = [NSURLRequest requestWithURL:urlToDownLoad cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20];
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection)
{
responseData = [[NSMutableData alloc] init];
[connection start];
}
else
{
[self finish];
}
if (connection != nil)
{
do
{
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
while (!done);
}
[pool release], pool = nil;
}
#pragma mark -
#pragma mark - NSURLConnectionDataDelegate methods
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[returnDataDic setObject:@"error" forKey:...];
[target performSelectorOnMainThread:action withObject:returnDataDic waitUntilDone:NO];
[self finish];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[returnDataDic setObject:responseData forKey:...];
[target performSelectorOnMainThread:action withObject:returnDataDic waitUntilDone:NO];
[self finish];
}
@end
楽器は私に漏れを与えました:[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
なぜですか?ありがとう!
操作で非同期ダウンロードを行いたいだけですが、を使用するNSAutoreleasePool
と、楽器がでリークを起こしました: [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
。