何时释放/保留传递给辅助线程的对象?

发布于 2024-08-21 00:58:06 字数 1045 浏览 8 评论 0原文

我使用以下代码将对象传递给辅助线程:

 (void)login:(id)sender
{
  platformMsgs_LoginRequest *loginRequest = [[[platformMsgs_LoginRequest alloc] init] autorelease];
//more code...
 [NSThread detachNewThreadSelector:@selector(sendLoginRequest:) toTarget:self withObject:loginRequest];
//more code...
}

- (void)sendLoginRequest:(platformMsgs_LoginRequest *)loginRequest
 {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 [loginRequest retain];
 NetSuiteBinding *binding = [NetSuiteServiceSvc NetSuiteBinding];
 NetSuiteBindingResponse *response = [binding loginUsingParameters:loginRequest      applicationInfo:nil partnerInfo:nil];
 [self performSelectorOnMainThread:@selector(loginOperationCompleted:)   withObject:response waitUntilDone:NO];
 [loginRequest release];
 [pool drain];
 }

我的问题是,autorelease 是处理该对象释放的正确方法吗?一旦它被传递到辅助线程,我就保留它并在不再需要它时释放它。

但是,自动释放是否有可能在辅助线程有机会保留该对象之前释放该对象?我是否必须为此创建一个ivar?以便我可以在performSelectorOnMainThread 中释放该对象?登录后我不再需要该对象,因此 ivar 似乎不是正确的方法。处理这个问题的最佳方法是什么?谢谢。

-奥斯卡

I am passing an object to a secondary thread using the following code:

 (void)login:(id)sender
{
  platformMsgs_LoginRequest *loginRequest = [[[platformMsgs_LoginRequest alloc] init] autorelease];
//more code...
 [NSThread detachNewThreadSelector:@selector(sendLoginRequest:) toTarget:self withObject:loginRequest];
//more code...
}

- (void)sendLoginRequest:(platformMsgs_LoginRequest *)loginRequest
 {
 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 [loginRequest retain];
 NetSuiteBinding *binding = [NetSuiteServiceSvc NetSuiteBinding];
 NetSuiteBindingResponse *response = [binding loginUsingParameters:loginRequest      applicationInfo:nil partnerInfo:nil];
 [self performSelectorOnMainThread:@selector(loginOperationCompleted:)   withObject:response waitUntilDone:NO];
 [loginRequest release];
 [pool drain];
 }

My question is, is autorelease the right way to handle the release of this object?. Once it is passed to the secondary thread I retain it and release it when I no longer need it.

However is it possible that the autorelease, releases the object before the secondary thread has a chance to retain it?. Do I have to create an ivar for this?, so that I can release the object in the performSelectorOnMainThread?. I no longer need the object after the login so an ivar doesn't seem like the right way to go. What is the best way to handle this?. Thank you.

-Oscar

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

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

发布评论

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

评论(2

末蓝 2024-08-28 00:58:06

detachNewThreadSelector:toTarget:withObject: 的文档回答了您的问题:

对象aTarget和anArgument在分离线程执行期间被保留,然后被释放。

所以是的,您可以自动释放该对象或在调用 detachNewThreadSelector 后显式释放它。并且您不必在辅助线程中保留该对象。

The documentation for detachNewThreadSelector:toTarget:withObject: answers your question:

The objects aTarget and anArgument are retained during the execution of the detached thread, then released.

So yes, you can autorelease the object or release it explicitly after calling detachNewThreadSelector. And you don't have to retain the object in the secondary thread.

远山浅 2024-08-28 00:58:06

来自文档。

分离NewThreadSelector:toTarget:withObject:

对象 aTarget 和 anArgument 是
执行期间保留
分离线程,然后释放。这
退出分离的线程(使用
一旦 aTarget 退出类方法
已完成 aSelector 的执行
方法。

所以你不需要这么努力。 Autorelease 在这里很好,您不需要将其保留在线程中,因为线程本身保留参数和目标,并在完成后释放它。

From the docs.

detachNewThreadSelector:toTarget:withObject:

The objects aTarget and anArgument are
retained during the execution of the
detached thread, then released. The
detached thread is exited (using the
exit class method) as soon as aTarget
has completed executing the aSelector
method.

So you dont need to try this hard. Autorelease is fine here, and you should not need to retain it in the thread since the thread itself retains the argument and the target, and will release it when done.

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