帮助我理解这段代码的泄漏:
可能的重复:
如何释放对象声明到一个方法中并传递给另一个方法?
你能帮我修复这段代码中的泄漏吗:
- (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
Possible Duplicate:
How to release an object declared into a method and passed to another method?
Can you help me fix leaks in this code:
- (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]];
}
Thx for helping,
Stephane
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您在
if
语句中设置picture = UIGraphicsGetImageFromCurrentImageContext()
或在else
语句中设置picture = Payload
时,您正在丢失指向您在第一行的picture
中分配的先前分配的 UIImage 的指针,但您从未释放它。您不应该为
picture
分配+初始化一个新的 UIImage,因为您从不使用它并稍后为该变量分配新值...但从未使用和释放之前分配的值。When you set
picture = UIGraphicsGetImageFromCurrentImageContext()
in yourif
statement orpicture = payload
in yourelse
statement, you are loosing the pointer to the previously allocated UIImage you assigned inpicture
in the first lines, but you never released it.You shouldn't alloc+init a new UIImage for
picture
, as you never use it and assign a new value to this variable later... but never used and released the previously allocated one.UIImage *picture = [[UIImage alloc] init];
和picture = UIGraphicsGetImageFromCurrentImageContext();
您不应在代码开头初始化
picture
。UIGraphicsGetImageFromCurrentImageContext
返回已初始化的UIImage
(自动释放)。UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
,图片=有效负载;
,[有效负载释放];
。您应该使用
[payload autorelease]
,否则您将释放图像而不使用它。[委托 ThumbDidLoad:self.indexPathInTableView Image:[图片自动释放]];
您应该删除
[图片自动释放]
并仅使用图片
。UIImage *picture = [[UIImage alloc] init];
andpicture = UIGraphicsGetImageFromCurrentImageContext();
You should not initialize
picture
at the beginning of the code.UIGraphicsGetImageFromCurrentImageContext
returns a already initializedUIImage
(autoreleased).UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
,picture = payload;
,[payload release];
.You should use
[payload autorelease]
instead, otherwise you're releasing the image without ever using it.[delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]];
You should remove
[picture autorelease]
and just usepicture
.