NS调用泄漏

发布于 2024-09-16 09:31:58 字数 1531 浏览 3 评论 0原文

我正在尝试设置一个 NSInovcation 系统,以使用 PerformSelectorInBackground 将选择器启动到后台线程中: - 到目前为止,在实例方法 (-) 上运行系统时一切都很成功,但我也想支持类方法 (+)。我已经调整了我的代码,为两种类型的类提供一个 invokeInBackgroundThread ,除了一个问题之外,一切正常。当调用类方法时,我的控制台充斥着“自动释放,没有池”消息。不知道是什么原因造成的。基于 DDFoundation 开源项目的代码如下所示。


@implementation NSObject (DDExtensions)
...
+ (id)invokeInBackgroundThread
{
    DDInvocationGrabber *grabber = [DDInvocationGrabber invocationGrabber];
    [grabber setInvocationThreadType:INVOCATION_BACKGROUND_THREAD];
    return [grabber prepareWithInvocationTarget:self];
}

- (id)invokeInBackgroundThread
{
    DDInvocationGrabber *grabber = [DDInvocationGrabber invocationGrabber];
    [grabber setInvocationThreadType:INVOCATION_BACKGROUND_THREAD];
    return [grabber prepareWithInvocationTarget:self];
}
...

...
- (void)forwardInvocation:(NSInvocation *)ioInvocation
{
    [ioInvocation setTarget:[self target]];
    [self setInvocation:ioInvocation];

 if (_waitUntilDone == NO) {
  [_invocation retainArguments];
 }

    if (_threadType == INVOCATION_MAIN_THREAD)
    {
        [_invocation performSelectorOnMainThread:@selector(invoke)
                                      withObject:nil
                                   waitUntilDone:_waitUntilDone];
    } else {
        [_invocation performSelectorInBackground:@selector(invoke)
                                  withObject:nil];
 }
}
...

+(void)doSomething;
[[className invokeOnBackgroundThread] doSomething];

I am trying to setup an NSInovcation system to launch selectors into background threads using performSelectorInBackground: - So far everything is successful when running the system on instance methods (-), but I also want to support class methods (+). I have adjusted my code to provide an invokeInBackgroundThread for both types of class and everything worked except for one problem. When the class methods are invoked I get my console flooded with "autoreleased with no pool in place" messages. No idea what is causing it. The code which is based off the DDFoundation open source project is shown below.


@implementation NSObject (DDExtensions)
...
+ (id)invokeInBackgroundThread
{
    DDInvocationGrabber *grabber = [DDInvocationGrabber invocationGrabber];
    [grabber setInvocationThreadType:INVOCATION_BACKGROUND_THREAD];
    return [grabber prepareWithInvocationTarget:self];
}

- (id)invokeInBackgroundThread
{
    DDInvocationGrabber *grabber = [DDInvocationGrabber invocationGrabber];
    [grabber setInvocationThreadType:INVOCATION_BACKGROUND_THREAD];
    return [grabber prepareWithInvocationTarget:self];
}
...

...
- (void)forwardInvocation:(NSInvocation *)ioInvocation
{
    [ioInvocation setTarget:[self target]];
    [self setInvocation:ioInvocation];

 if (_waitUntilDone == NO) {
  [_invocation retainArguments];
 }

    if (_threadType == INVOCATION_MAIN_THREAD)
    {
        [_invocation performSelectorOnMainThread:@selector(invoke)
                                      withObject:nil
                                   waitUntilDone:_waitUntilDone];
    } else {
        [_invocation performSelectorInBackground:@selector(invoke)
                                  withObject:nil];
 }
}
...

+(void)doSomething;
[[className invokeOnBackgroundThread] doSomething];

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

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

发布评论

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

评论(1

夏末染殇 2024-09-23 09:31:58

默认情况下,主线程具有自动释放池,如果您启动额外的线程 - 创建该池是您的工作。实际上,这里没什么复杂的,只是

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// Work...
[pool release];

另外,如果你有很多线程,我建议你看看 NSOperation 而不是使用 [performSelectorInBackground] 运行线程。 NSOperation(带有包装队列)是此类任务的更灵活的解决方案。

Main thread has autorelease pool by default, if you start extra thread - it's your job to create the pool. Actually, nothing complicated here, just

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
// Work...
[pool release];

Also, if you have a lot of threads, I'd suggest you to take a look at NSOperation instead of running threads with [performSelectorInBackground]. NSOperation (with wrapping queue) is more flexible solution for such tasks.

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