iPhone 自动释放池和分配
我一直在阅读有关 autoreleasepool 的内容,但有一点我不太清楚。我有一些使用线程的功能,需要使用自动释放池进行单独的内存管理。
下面的例子是正确的
-(void) doSomething {
NSAutorelease *pool = [[NSAutorelasepool alloc] init];
NSString *myString = @"Hello";
[pool release];
}
这是正确的吗?
-(void) doSomething {
NSAutorelease *pool = [[NSAutorelasepool alloc] init];
NSString *myString = [[NSString alloc] initWithString:@"Hello"];
[pool release];
}
或者这个?
-(void) doSomething {
NSAutorelease *pool = [[NSAutorelasepool alloc] init];
NSString *myString = [[NSString alloc] initWithString:@"Hello"];
[myString release];
[pool release];
}
我的问题是在自动释放池范围内创建的拥有的对象需要专门释放,或者在释放自动释放池时是否得到处理?
特奥
I've been reading about autoreleasepool but there is a point which is a bit unclear to me. I have some functionality using threads that required seperate memory managment using autoreleasepool.
In the following example is correct
-(void) doSomething {
NSAutorelease *pool = [[NSAutorelasepool alloc] init];
NSString *myString = @"Hello";
[pool release];
}
Is this correct?
-(void) doSomething {
NSAutorelease *pool = [[NSAutorelasepool alloc] init];
NSString *myString = [[NSString alloc] initWithString:@"Hello"];
[pool release];
}
or this?
-(void) doSomething {
NSAutorelease *pool = [[NSAutorelasepool alloc] init];
NSString *myString = [[NSString alloc] initWithString:@"Hello"];
[myString release];
[pool release];
}
My question is owned objects created in the scope of the autorelease pool need to be relased specifically or are the taken care of when the autorelasepool is been released?
Teo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
自动释放池处理自动释放的对象。如果您拥有一个对象(通过分配或复制或保留),那么您必须释放它。所以你的第二个例子是不正确的。由于您已经分配了该字符串,因此您拥有它并且必须释放它。
为主线程创建一个自动释放池。 (如果需要,您可以查看 main 函数)。每个线程都需要自己的自动释放池来管理自动释放的对象。这就是为什么如果您创建另一个线程,则必须为该线程创建一个自动释放池。即使您不在线程中创建自动释放对象,您也应该创建它,因为该线程中的库调用可能会创建自动释放对象。即使您确定没有库调用正在创建自动释放的对象,那么您也应该创建它们,因为这是最佳实践,特别是如果您正在处理由多人开发和维护的大型项目。
Autorelease pool handles the autoreleased objects. If you own an object (via alloc or copy or retain) then you must release it. So your 2nd example is not correct. As you have allocated the string, you own it and you must release it.
An autorelease pool is created for the main thread. (You can look into the main function if you want). Every thread need its own autorelease pool to manage autoreleased objects. That's why if you create another thread then you must create an autorelease pool for that thread. Even if you don't create autoreleased object in the thread, you should create this as the library calls in that thread may create autoreleased objects. Even if you are sure that no library calls are making autoreleased objects then you also should create them as that is the best practice, specially if you are working on big project which is developed and maintained by multiple people.
当你创建一堆时,你只需要创建自己的自动释放池
您想要立即进行垃圾收集的自动释放对象。但是,您是正确的,因为您不想引用释放池后创建的任何“自动释放”对象。当池耗尽时,自动释放的对象(您不保留的对象)将被销毁。
由于示例中的任何对象都不是自动释放的,因此创建自己的自动释放池本质上是无操作的。
You only need to create your own autorelease pool when you are creating a bunch of
autoreleased objects you want to garbage collect immediately. However, you are correct in that you don't want to reference any "autoreleased" objects you create after you release the pool. Autoreleased objects (which you don't retain) are destroyed when the pool is drained.
Since none of the objects in your example are autoreleased, creating your own autorelease pool is essentially a no-op.
您的两个示例都不需要自动释放池。自动释放池仅处理自动释放的对象:
您的第一个字符串是使用字符串文字初始化的,因此在内存管理方面可能是特殊的(也许其他人知道更多)。你的第二根弦泄漏了,池子没有什么区别。您的第三个字符串已正确释放,池也没有任何区别。
这就是您需要池的地方:
如果不存在池,
foo
字符串就会泄漏。请注意,我在池上调用drain
而不是release
- 在 iOS 上没有区别,但在垃圾收集环境中两者不同,因此最好养成拨打正确电话的习惯。另请注意,即使您自己不自动释放任何对象,您也可能需要一个池,您在方法中调用的代码中的某个位置可能会执行许多内存操作。
Neither of your examples needs an autorelease pool. Autorelease pools only take care of autoreleased objects:
Your first string is initialized using a string literal and therefore is probably special with respect to memory management (maybe someone else knows more). Your second string leaks, the pool does not make a difference. Your third string is released correctly, again the pool does not make a difference.
This is where you would need a pool:
Here the
foo
string would leak if the pool wasn’t there. Note that I’m callingdrain
instead ofrelease
on the pool – on iOS there’s not a difference, but in garbage-collected environments the two differ, so it’s probably better to get in the habit of calling the right one.Also note that you may need a pool even though you don’t autorelease any objects yourself, there could be many memory operations done somewhere in the code you’re calling in your method.
认为这应该是这样的:
您必须向自动释放池内的对象发送自动释放消息。当释放消息发送到池时它们将被释放。
Think that this should be something like this:
You must send autorelease message, to objects inside autorelease pool. They will be released when release message is sent to pool.