IPhone SDK - Leaking Memory with performSelectorInBackground

前端 未结 2 1162
暖寄归人
暖寄归人 2020-12-12 07:15

Maybe someone can help me with this strange thing:

If a user clicks on a button, a new UITableView is pushed to the navigation controller. This new view is doing som

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

    Btw, you're calling pushViewController: from a background thread. This is bad.

    You should only do things to the UI - like pushing view controllers and changing UI items - from the main thread. If you don't, things break.

    See the Cocoa Fundamentals Guide section titled "Are the Cocoa Frameworks Thread Safe?": it says "All UIKit objects should be used on the main thread only."

    0 讨论(0)
  • 2020-12-12 07:32

    No, small memory leaks will not (most likely) you application to be rejected from appstore.

    In your example as you run your method in separate thread you should create and dispose NSAutoreleasePool object for that thread to handle autoreleased objects. Following changes to getController method should do the trick:

    -(void) getController {
        NSAutoreleasedPool *pool = [[NSAutoreleasedPool alloc] init];
    
        [self.workController loadList]; // Does the DB Query
        [self.navigationController pushViewController:self.workController animated:YES];
    
        [pool release];
    }
    

    For more details see Autorelease Pools section in memory management guide. Relevant quote from there:

    If you spawn a secondary thread, you must create your own autorelease pool as soon as the thread begins executing; otherwise, you will leak objects. (See “Autorelease Pools and Threads” for details.)

    0 讨论(0)
提交回复
热议问题