views:

1556

answers:

3

Hello every one, I want to create an appilication in iPhone in which I want to use NSThread. I have created one thread using

[NSThread detachNewThreadSelector:@selector(doThread:)
           toTarget:self
            withObject:nil];

I want that my one thread will handle all the touches and other user interaction and the second thread handle the NSTimer. So, In doThread() I have allocate NSTimer like,

-(void) doThread:(NSString *)poststring {

    NSLog(@"create thread:");

    [lock lock];
    T1 = [NSTimer scheduledTimerWithTimeInterval:(5)   
     target : self
     selector:@selector(onTimer)
     userInfo : nil
     repeats : YES];
     NSLog(@"after timer");

    usleep(1);
    [lock unlock];
}

In onTImer,

-(void)onTimer

{
    NSLog(@"in timer");

}

Now I can't able to call the onTimer method of NSTimer. But I can see the "after timer" printed in the log.Is that anything that I can't use the NSTimer within the thread?

This is also I can get while execution.

NSAutoreleaseNoPool(): Object 0xd15880 of class __NSCFDate autoreleased with no pool in place - just leaking
Stack: (0x305a2e6f 0x30504682 0x30525acf 0x27b5 0x3050a79d 0x3050a338 0x926ae155 0x926ae012)

Please help me for that. Thank you.

A: 

I'm not sure I understand your question about the onTimer method. Can you restate it?

As for:

NSAutoreleaseNoPool(): Object 0xd15880 of class __NSCFDate autoreleased with no pool in place - just leaking

A few things can cause this:

If you're not delegating or subclassing an UIApplication object, you won't have an autorelease pool in place and would have to create one on your own. However, the right answer in that case is just to be sure you're using UIApplication correctly.

In this case however, since you're detaching the thread, that's likely the cause for the error. Detached threads don't have autorelease pools, so you'd have to create your own.

See the documentation:

Autorelease Pools

Wade Williams
+5  A: 

NSTimer schedules its time events on the current NSRunLoop--your thread doesn't start one.

If all you are trying to do is run something after a certain amount of time, use -[NSObject performSelector:withObject:afterDelay:]:

[self performSelector:@selector(onTimer) withObject:nil afterDelay:5.0f];

If you are trying to actually do work in the background, +[NSThread detachNewThreadSelector:toTarget:withObject:] will work as expected but you shouldn't run timer events in the background without an NSRunLoop. Also, you will need to wrap your code in an autorelease pool:

- (void)doThread:(NSString *)poststring
{
     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     // Your code goes in here
     [pool drain];
}
rpetrich
A: 

thanks for your answer..thanks.

www.ruu.cc