objective c - iOS memory leak in thread -


after running thread while, instruments shows __nsdate has steadily incementing # living value.

my conclusion tread not dealocate objects. line, however, causes compilation error nsautoreleasepool *pool = [[nsautoreleasepool alloc] init];

how can force thread retain objects or how shall create proper thread working arc.

- (void) start {    nsthread* mythread = [[nsthread alloc] initwithtarget:self                                           selector:@selector(mythreadmainroutine)                                           object:nil];    [mythread start];  // create thread }  - (void)mythreadmainroutine {     // stuff inits here ...      // thread work here.    while (_live) {        // stuff ...        [runloop rununtildate:[nsdate datewithtimeintervalsincenow:0.05]];               [nsthread sleepfortimeinterval:0.05f];    }     // clean stuff here ...  } 

the autoreleased objects reason increasing memory usage, cannot use nsautoreleasepool arc. replace

nsautoreleasepool *pool = [[nsautoreleasepool alloc] init]; // ... [pool drain]; 

with

@autoreleasepool {     // ... } 

update: need 2 autorelease pools in case. first of all, threading programming guide states:

if application uses managed memory model, creating autorelease pool should first thing in thread entry routine. similarly, destroying autorelease pool should last thing in thread. pool ensures autoreleased objects caught, although not release them until thread exits.

and last sentence gives clue why need autorelease pool: otherwise autoreleased objects created in long-running loop released when thread exits. have

- (void)mythreadmainroutine {     @autoreleasepool {         // stuff inits here ...         while (_live) {             @autoreleasepool {                 // stuff ...                 [runloop rununtildate:[nsdate datewithtimeintervalsincenow:0.05]];                         [nsthread sleepfortimeinterval:0.05f];             }         }         // clean stuff here ...     } } 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -