아래 코드는 스레드를 포함합니다.이 스레드는 크기가 10보다 크면 로그를 남기고 마지막 객체를 제거합니다. 그러나 데모 = [[myDemo alloc] init]을 실행하면 다음과 같이됩니다. 스레드를 시작하고 예외 메시지 = "EXC_BAD_ACCESS"를 얻습니다. 어떤 사람이이 문제를 해결하도록 도와 주시겠습니까?NSThread 크래시 [EXC_BAD_ACCESS]
@interface myDemo:NSObject
{
NSMutableArray *q;
NSThread *thread;
bool running;
}
-(void)putData:(NSData *)data;
-(NSData *)popData;
-(void)stopThread;
@end;
@implementation myDemo
-(id)init
{
if(NULL!=(self = [super init]))
{
q=[NSMutableArray array];
thread=[[NSThread alloc] initWithTarget:self
selector:@selector(myThreadMainMethod:)
object:nil];
[thread start];
}
return self;
}
-(void)myThreadMainMethod:(id)object
{
unsigned long count;
NSData *data;
if(running) return;
running=true;
while(running)
{
@synchronized(self)
{
count=[q count];//crash !!!!
if(count>10)
{
data=[q lastObject];
NSLog(@"count=%d ,remove last data=%@",count,[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]);
[q removeLastObject];
}
}
}
running=false;
}
putData 및 popData가 +1와 "Q"바르 이렇게 비 오토 릴리즈 객체 카운트 유지 초기화하는
-(void)putData:(NSData *)data
{
@synchronized(self)
{
[q addObject:data];
}
}
-(NSData *)popData
{
NSData * data=NULL;
unsigned long count;
@synchronized(self)
{
count=[q count];
if(count!=0)
{
data=[q lastObject];
[q removeLastObject];
}
}
return data;
}
'q'는 어떻게 선언합니까? –
ARC를 사용하고 있습니까? –
@ Daij-Djan 그는 "[[NSString alloc] initWithData : data encoding : NSUTF8StringEncoding] autorelease] 행을 봅니다." –