可能重复:
How to release an object declared into a method and passed to another method?
你能帮我解决这段代码中的漏洞:
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
UIImage *picture = [[UIImage alloc] init];
if (payload.size.width != kAppIconHeight && payload.size.height != kAppIconHeight)
{
CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[payload drawInRect:imageRect];
picture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else
{
picture = payload;
}
self.activeDownload = nil;
[payload release];
self.imageConnection = nil;
[delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]];
}
求助,
的Stephane
答案 0 :(得分:4)
当您在picture = UIGraphicsGetImageFromCurrentImageContext()
语句中设置if
或在picture = payload
语句中设置else
时,您将丢失指向您在{{1}中分配的先前分配的UIImage的指针在第一行,但你从未发布它。
你不应该为picture
分配+ init一个新的UIImage,因为你从来没有使用它,并且稍后为这个变量分配一个新的值......但是从未使用过并释放过以前分配的那个。
答案 1 :(得分:0)
UIImage *picture = [[UIImage alloc] init];
和picture = UIGraphicsGetImageFromCurrentImageContext();
您不应该在代码的开头初始化picture
。 UIGraphicsGetImageFromCurrentImageContext
返回已初始化的UIImage
(已自动发布)。
UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
,picture = payload;
,[payload release];
。
您应该使用[payload autorelease]
,否则您将在不使用图片的情况下发布图片。
[delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]];
您应该删除[picture autorelease]
并使用picture
。
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
UIImage *picture;
if (payload.size.width != kAppIconHeight && payload.size.height != kAppIconHeight)
{
CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[payload drawInRect:imageRect];
picture = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
else
{
picture = payload;
}
[payload autorelease];
self.activeDownload = nil;
self.imageConnection = nil;
[delegate ThumbDidLoad:self.indexPathInTableView Image:picture];
}