为什么这个 Objective-C IOS 代码没有泄露?

发布于 2024-12-23 20:13:07 字数 751 浏览 3 评论 0原文

我正在重复使用 surl,所以看起来它会泄漏。但仪器不显示链接。我一定不明白它如何在不创建垃圾字符串的情况下重用它。

NSString *rcode = @"DDD";

NSString *surl = [NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rcode, @"7"];
NSURL *url = [NSURL URLWithString: surl]; 
UIImage *image7 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 

surl = [NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rrcode, @"6"];
url = [NSURL URLWithString: surl]; 
UIImage *image6 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 

surl = [NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rrcode, @"5"];
url = [NSURL URLWithString: surl]; 
UIImage *image5 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 

I am reusing surl so it seems like it would be leaking. But instruments does not show a link. I must not be understanding how it can reuse it without creating garbage strings.

NSString *rcode = @"DDD";

NSString *surl = [NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rcode, @"7"];
NSURL *url = [NSURL URLWithString: surl]; 
UIImage *image7 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 

surl = [NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rrcode, @"6"];
url = [NSURL URLWithString: surl]; 
UIImage *image6 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 

surl = [NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rrcode, @"5"];
url = [NSURL URLWithString: surl]; 
UIImage *image5 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]]; 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薔薇婲 2024-12-30 20:13:07

看这一行:

[NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rcode, @"7"];

+[ NSString stringWithFormat:] 是一个“便捷构造函数”,有时称为“便捷方法”,它返回一个不属于您的 NSString,因此您没有任何责任释放它,除非您选择保留它你自己(你没有)。

基本规则是,如果您从以“alloc”、“new”、“copy”或“mutableCopy”开头的方法获取一个对象,那么您有责任释放它。否则,你就不会。

无论有或没有 ARC,我都没有看到泄漏。

Look at this line:

[NSString stringWithFormat: @"http://www.myweather.com/%@_%@.png", rcode, @"7"];

+[NSString stringWithFormat:] is a "convenience constructor", sometimes called a "convenience method", that returns an NSString that you do not own, so you don't have any responsibility to release it unless you choose to retain it yourself (which you didn't).

The basic rule is if you get an object from a method that begins with “alloc”, “new”, “copy”, or “mutableCopy”, then you have a responsibility to release it. Otherwise, you don't.

I don't see a leak here with or without ARC.

镜花水月 2024-12-30 20:13:07

该代码不会泄漏,因为它使用“自动释放”对象。

您编写的所有对象创建代码都使用返回自动释放对象的便捷方法。这意味着当相应的自动释放池被释放时,这些将被释放。

请参阅 Apple 的 高级内存管理中的此页面编程指南了解有关自动释放的更多信息。

This code is not leaking because it is using 'autoreleased' objects.

All the object creation code you have written is using convenience methods which return autoreleased objects. This means that these will be released when the corresponding autorelease pool will get released.

Please refer to this page in Apple's Advanced Memory Management Programming Guide to know more about autorelease.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文