Why is there no autorelease pool when I do performSelectorInBackground:?

前端 未结 2 787
小鲜肉
小鲜肉 2020-12-30 15:39

I am calling a method that goes in a background thread:

[self performSelectorInBackground:@selector(loadViewControllerWithIndex:) withObject:[NSNumber number         


        
相关标签:
2条回答
  • 2020-12-30 16:07

    Most likely the reason for this is because the leaked object (an NSNumber), is a parameter passed in from outside the thread. Hence, this variable belongs to the calling thread (and its autorelease pool)

    The reason that the autorelease pool around the thread call doesn't work, is because the thread creator (performSelectorInbackground) - returns immediately, most likely while the thread is still running.

    I suggest you do a release on your selector's parameter after passing it in as an argument.

    0 讨论(0)
  • 2020-12-30 16:11

    I agree that most likely the reason for this is because the leaked object (an NSNumber), is a parameter passed in from outside the thread. Hence, this variable belongs to the calling thread (and its autorelease pool)

    The calling thread should use NSAutoreleasePool and I suggest that you add a retain instruction to your parameter as:

    - (void) loadViewControllerWithIndex:(NSNumber *)indexNumberObj {
        NSAutoreleasePool *arPool = [[NSAutoreleasePool alloc] init];
        [indexNumberObj retain];
    
        ....
    
        [arPool release];
      }
    
    0 讨论(0)
提交回复
热议问题