NSThread with _NSAutoreleaseNoPool error

前端 未结 5 604
独厮守ぢ
独厮守ぢ 2021-02-04 09:38

I have an method which save files to the internet, it works but just slow. Then I\'d like to make the user interface more smooth, so I create an NSThread to handle the slow task

5条回答
  •  灰色年华
    2021-02-04 10:19

    You need to mainly create an autorelease pool for the thread. Try changing your save method to be like this:

    - (void) save:(id)arg {
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
        //Existing code
    
        [pool drain];
    }
    

    You will not you that the above does not call release on the NSAutoreleasePool. This is a special case. For NSAutoreleasePool drain is equivalent to release when running without GC, and converts to a hint to collector that it might be good point to run a collection.

提交回复
热议问题