2012-02-11 4 views
2
내가 XCode4에서 다음 코드를 사용하여 멀티 스레딩 작업입니다

에는 풀과 오토 릴리즈 :개체 장소

#import <Foundation/Foundation.h> 

bool trigger = false; 
NSLock *theLock=[NSLock new]; 

@interface Metronome : NSObject 
+(void)tick:(id)param; 
@end 

@implementation Metronome 
+(void)tick:(id)param{ 
while(1) 
    { 
      NSLog(@"TICK\n"); 
      usleep(1000000); 
      [theLock lock]; 
      trigger = true; 
      [theLock unlock]; 
    } 
} 
@end 

int main() 
{ 
    [NSThread detachNewThreadSelector:@selector(tick:) 
toTarget:[Metronome class] withObject:nil]; 
} 

더 컴파일 오류가 없다, 그러나 실행 중에 콘솔은 다음과 같은 경고를 팝업 :

objc[688]: Object 0x100300ff0 of class NSThread autoreleased with no pool 
in place - just leaking - break on objc_autoreleaseNoPool() to debug 

obj-C의 메모리 관리에 익숙하지 않습니다. 누군가 나에게 이것을 설명 할 수 있습니까? 고마워요!

답변

1

당신은 autorelease 메인 스레드를 포함 호출 할 필요가 모든 스레드에 대한 NSAutoreleasePool을 만들 필요가

int main() 
{ 
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 
    [NSThread detachNewThreadSelector:@selector(tick:) 
toTarget:[Metronome class] withObject:nil]; 
    [pool release]; 
}