Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective-C Threading: Exiting a thread, retain problems
    text
    copied!<p>I'm trying to create a thread that configures a run loop to run a physics engine through a defined NSTimer. However, I'm having trouble making the thread exit normally (or I think the problem is).</p> <p>Attached are the relevant portions of my code:</p> <p>(This code is in a view controller) (back is called when a button is pressed)</p> <pre><code>- (void)back { [timestep invalidate]; exiting = YES; [self release]; } - (void)initializePhysicsWorld { // Initializes the thread to simulate physics interactions. [NSThread detachNewThreadSelector:@selector(physicsThreadMethod) toTarget:self withObject:nil]; } - (void)physicsThreadMethod { NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSRunLoop *myRunLoop = [NSRunLoop currentRunLoop]; timestep = [NSTimer timerWithTimeInterval:1.0f/60.0f target:self selector:@selector(step:) userInfo:nil repeats:YES]; [myRunLoop addTimer:timestep forMode:NSDefaultRunLoopMode]; while (!exiting) { CFRunLoopRun(); [pool release]; pool = [[NSAutoreleasePool alloc] init]; // periodically refreshes pool } CFRunLoopStop([myRunLoop getCFRunLoop]); NSLog(@"Thread is going to exit"); [pool release]; } - (void)dealloc { if ([self.view superview]) { [self.view removeFromSuperview]; } [super dealloc]; } </code></pre> <p>The engine (the step: function) runs fine, but when I try to exit the loop by running the method back, it would appear that the thread does not release its retain on my view controller (dealloc is not called). I think my thread didn't exit the physicsThreadMethod method as the NSLog does not appear in the console. Dealloc was only called when I run 'back' a second time.</p> <p>I'm not really sure why this is happening, so I would really appreciate any help. Thanks!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload