通过分配保留 self 对象是个好主意吗

发布于 2024-12-25 21:13:52 字数 407 浏览 7 评论 0原文

我正在做一些分配,因为

self.xyz = [[NSDictionary alloc] init];

保留这样的财产是个好主意吗?还是

这样做会更好,例如:

NSDictionary *zzz = [[NSDictionary alloc] init];
self.xyz = zzz;
[zzz release];

我在这里关心的是,我看到一些地方有人保留,例如:

self.xyz = [[NSDictionary alloc] init];

这意味着保留计数是 2。那么将这里的计数减少到 1 的最佳方法是什么。

谢谢。只是想更清楚地了解一些内存管理概念。

I am doing some allocation as

self.xyz = [[NSDictionary alloc] init];

Is it a good idea of retaining a property such like that? Or

will it be better to do such as:

NSDictionary *zzz = [[NSDictionary alloc] init];
self.xyz = zzz;
[zzz release];

My concern here is, I have seen some places people retaining such as:

self.xyz = [[NSDictionary alloc] init];

which means the retain count is 2. So what's the best way to reduce a count here to one.

Thanks. Just trying to clear some memory management concept clear a little more.

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

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

发布评论

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

评论(7

岁月无声 2025-01-01 21:13:52

这取决于您处理属性xyz的方式。如果您

@property (nonatomic, retain) NSObject *xyz;

这样做,则 xyzNSDictionary 上有一个保留,因此第二个(3 行)版本是最好的。

This depends on how you've handled the property xyz. If you did

@property (nonatomic, retain) NSObject *xyz;

then xyz has a retain on the NSDictionary, so the second (3 line) version is best.

苍暮颜 2025-01-01 21:13:52

我通常会这样做:

self.xyz = [[[NSDictionary alloc] init] autorelease];

或者,如果该类有一个方便的方法,则像这样使用它:

self.xyz = [NSDictionary dictionary];

或者,只使用 ARC 并让它在这种情况下为您完成工作。

I usually would do this:

self.xyz = [[[NSDictionary alloc] init] autorelease];

Or if the class has a convenience method then use it like so:

self.xyz = [NSDictionary dictionary];

Or, just use ARC and let it do the work for you in this instance.

瑶笙 2025-01-01 21:13:52

我假设我们正在讨论不使用 ARC 时的最佳实践。

在手动内存管理环境中,第一种方法是完全错误的,因为正如您所指出的,保留计数为 2。执行单行的正确方法如下所示:

self.xyz = [[[NSDictionary alloc] init] autorelease];

遍历综合设置器(假设 xyz 是用保留指令声明的),除了 alloc/init 添加 1 之外,还向保留计数添加 1。自动释放可以平衡这个问题。

您详细介绍的第二种方法在功能上是等效的,但由于可用内存相对较少,因此被认为是嵌入式设备上的更好实践。您创建一个对象,将其分配给属性并立即释放原始临时对象。在前一种方法中,对象被放入自动释放池中并在稍后的时间点释放。

I'm assuming we're talking about best practices when NOT using ARC.

In a manual memory management environment, the first approach is flat out wrong because as you pointed out, the retain count is at 2. The proper way to do a one liner is like so:

self.xyz = [[[NSDictionary alloc] init] autorelease];

Going through the synthesized setter (assuming xyz is declared with the retain directive), adds 1 to the retain count in addition to the 1 being added by alloc/init. The autorelease is there to balance this out.

The second approach that you detailed is functionally equivalent, but considered a better practice on embedded devices because of the relatively small amounts of memory available. You create an object, assign it to the property and release the original temporary object immediately. In the former approach, the object is placed into an autorelease pool and released at a later point in time.

梦断已成空 2025-01-01 21:13:52

我认为您应该使用自动释放作为代码的最佳实践。

I think you should use autorelease as a best practices for your code.

春风十里 2025-01-01 21:13:52

第二种方法最好将保留计数保持为 1。

The second approach is the best to maintain retain count as 1.

酒儿 2025-01-01 21:13:52

您必须始终平衡内存管理等式,否则可能会出现内存泄漏。

在您的示例中,它取决于 xyz 内存管理策略。

如果 xyz 有保留策略,则保留计数为 2。这是一个常见的错误,并且存在内存泄漏。

@property (retain, nonatomic) SomeClass* xyz;

如果 xyz 有分配策略,则不会增加保留计数

@property (assign, nonatomic) SomeClass* xyz;

总之,如果您使用保留策略,则您提供的第二个片段是正确的方法。显然你必须记住在 dealloc 方法中释放该属性。

- (void)dealloc
{
   [xyz release];     
   [super dealloc];
}

希望有帮助。

You have always to balance memory-management equation, if not, you could have memory leaks.

In your example it depends on xyz memory management policy.

If xyz has a retain policy, you have a retain count of two. It's a common mistake and you have a memory leak.

@property (retain, nonatomic) SomeClass* xyz;

If xyz has an assign policy, you don't increment the retain count

@property (assign, nonatomic) SomeClass* xyz;

In conclusion, if you use a retain policy, the second snippet you provided is the right way. Obviously you have to remember to release that property in dealloc method.

- (void)dealloc
{
   [xyz release];     
   [super dealloc];
}

Hope it helps.

扎心 2025-01-01 21:13:52

依我愚见,

self.xyz = [NSDictionary alloc]

会造成内存泄漏。需要手动释放。

你可以这样做

xyz = [NSDictionary alloc]

之后,即使我也有同样的问题。哪种方法更好?为什么?

In my humble opinion,

self.xyz = [NSDictionary alloc]

will cause memory leak. You need to release manually.

You can do this

xyz = [NSDictionary alloc]

After that even I have the same question. Which approach is better? and why?

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