What does “Autoreleased with no pool in place” mean?

前端 未结 2 878
既然无缘
既然无缘 2021-02-15 14:36

My Application structure is as follows, the core part is written in C++ and using thread heavily, and i am developing UI in Objective C on top of it, if i don\'t execute the t

2条回答
  •  长情又很酷
    2021-02-15 14:56

    See these docs for what you should know about multithreading with Cocoa: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafetySummary/ThreadSafetySummary.html

    It's OK to design your app like you have, but two things should be kept in mind:

    1. Life is simplest (and sometimes necessary) when UI controls like views (AppKit or UIKit) are manipulated on the main thread. You can use Foundation objects and some AppKit/UIKit objects on background threads, and some Foundation objects can be used from multiple threads.
    2. If you're using any Cocoa objects at all in background threads, you'll need to set up autorelease pools on those threads.

    Like so:

    - (void)backgroundThreadStart 
    {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
        // do stuff
    
        [pool release];
    }
    

    That will fix your console errors, but you might have other issues that led to the actual crashing you were seeing.

提交回复
热议问题