2013-06-27 134 views
-1

下面是我的代码包含一个线程。此线程照顾队列大小,如果大小> 10,然后记录并删除最后一个对象。但是当我运行demo = [[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是由@synchronized(个体)访问队列

-(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; 
} 
+0

如何声明'q'? –

+0

你用ARC吗? –

+0

@ Daij-Djan他没有看到“[[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease]” –

回答

1

尝试初始化 “Q” 的ivar用1保持计数非自动释放物体,像这样:

- (id)init 
{ 
    if (self = [super init]) 
    { 
     q = [[NSMutableArray alloc] initWithCapacity:10]; 
     thread = [[NSThread alloc] initWithTarget:self 
             selector:@selector(myThreadMainMethod:) 
              object:nil]; 
     [thread start]; 
    } 
    return self; 
} 

此外,你必须把后台线程上运行的所有代码放入@autoreleasepoolNSAutoreleasePool。我认为你的程序不知何故耗尽内存。例如:

- (void)myThreadMainMethod:(id)object 
{ 
    @autoreleasepool { 
     static unsigned long count; 
     NSData *data = nil; 
     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; 
     } 
    } 
} 

此外,您的班级中的ivars同步存在问题。 您正在同步self,但您在同步范围之外使用“正在运行”。 此外,循环的逻辑不清楚,你只是运行循环一次,你为什么需要它?

+0

我测试你的建议,但崩溃仍然 – luckfox0927

+0

@ luckfox0927我已编辑我的答案,检查出 –

+0

感谢您的帮助!这个标志“运行”应该是“while循环”。 – luckfox0927

1

AFAIK [NSArray array]返回一个自动释放对象。虽然我只是没有找到一个参考。我认为你应该在init方法中保留它,因为你不需要ARC。

0

我重写线程代码below.thread是后死机 “while循环” 跑564次

-(void)myThreadMainMethod:(id)object 
    { 
     unsigned long count,index=0; 
     NSData *data; 
     NSMutableArray *q1; 
     if(running) return; 
     running=true; 

     q1=[NSMutableArray array]; 
     while(running) 
     { 
      @synchronized(self) 
      { 

       count=[q count];//crash !!! 
       NSLog(@"#%d count=%d ",index++,count); 

      } 
     } 
    } 

然后我再次如下

-(void)myThreadMainMethod:(id)object 
    { 
     unsigned long count,index=0; 
     NSData *data; 
     NSMutableArray *q1; 
     if(running) return; 
     running=true; 

     q1=[NSMutableArray array]; 
     while(running) 
     { 
      @synchronized(self) 
      { 

       count=[q1 count];//run fine 
       NSLog(@"#%d count=%d ",index++,count);  
      } 
     } 
    } 

运行良好......为什么改写?

+0

尝试编辑您的原始问题,而不是在没有实际答案的情况下为自己的问题添加答案。 你的问题怎么样: 你正在以非常错误的方式管理线程。尝试阅读本指南:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html –

0

是不是因为 NSMutableArray * q1;应该用作count = [q1 count];而不是count = [q count];因为它被宣布为q1而不是q?

相关问题