2010-10-18 77 views
1

我有一個視圖控制器是NSStreamDelegate,當視頻從導航控制器彈出而某些內容正在流式傳輸時出現問題,我得到一個「EXC_BAD_ACCESS」錯誤。我試圖關閉流,但它似乎並沒有阻止它,如果有一個流正在進行。處理這個問題的正確方法是什麼?如果某些事情正在流傳,你能否延緩觀點的發作?NSStream在閱讀時發佈,「EXC_BAD_ACCESS」iPhone SDK

#import "CameraViewer.h" 

@implementation CameraViewer 
@synthesize camService; 
@synthesize currentDownload; 

// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad. 
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil theService:(NSNetService *)cameraService { 
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) { 
     // Custom initialization 
     [self setCamService:cameraService]; 
    } 
    return self; 
} 



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib. 
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    [self downloadAgain]; 
} 

- (void)viewWillDisappear:(BOOL)animated{ 
    NSLog(@"view is going away"); 
    NSInputStream *istream; 
    [camService getInputStream:&istream outputStream:nil]; 
    [istream close]; 
    NSLog(@"view is gone"); 
    [super viewWillDisappear:animated]; 
} 


- (void)downloadAgain{ 
     NSInputStream *istream; 
     [camService getInputStream:&istream outputStream:nil]; 
     [istream retain]; 
     [istream setDelegate:self]; 
     [istream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode]; 
     [istream open]; 
} 

#pragma mark NSStream delegate method 

- (void)stream:(NSStream *)aStream handleEvent:(NSStreamEvent)event { 
     switch(event) { 
      case NSStreamEventHasBytesAvailable: 
       NSLog(@"Reading Stream"); 
       if (![self currentDownload]) { 
        [self setCurrentDownload:[[NSMutableData alloc] initWithCapacity:409600]]; 
       } 
       uint8_t readBuffer[4096]; 
       int amountRead = 0; 
       NSInputStream * is = (NSInputStream *)aStream; 
       amountRead = [is read:readBuffer maxLength:4096]; 
       [[self currentDownload] appendBytes:readBuffer length:amountRead]; 
       //NSLog(@"case 1"); 
       break; 
      case NSStreamEventEndEncountered: 
       [(NSInputStream *)aStream close]; 
       UIImage *newImage = [[UIImage alloc] initWithData:[self currentDownload]]; 
       [self setCurrentDownload:nil]; 
       if(newImage != nil){ 
        [imageView setImage:newImage]; 
       } 
       [newImage release]; 
       [self performSelector:@selector(downloadAgain) withObject:nil afterDelay:0.25]; 
       break; 
      default: 
       break; 
     } 
} 

// Override to allow orientations other than the default portrait orientation. 
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { 
    return YES; 
} 


- (void)didReceiveMemoryWarning { 
    // Releases the view if it doesn't have a superview. 
    [super didReceiveMemoryWarning]; 

    // Release any cached data, images, etc that aren't in use. 
} 

- (void)viewDidUnload { 
    [super viewDidUnload]; 
    // Release any retained subviews of the main view. 
    // e.g. self.myOutlet = nil; 
} 


- (void)dealloc { 
    [[self camService] release]; 
    [[self currentDownload] release]; 
    [super dealloc]; 
} 


@end 
+0

發佈您的代碼。 – 2010-10-18 19:08:11

+0

我發佈了代碼。 – Matt 2010-10-19 00:50:47

回答

0

我看到你撥打scheduleInRunLoop。在這種情況下,當你不需要流,你也應該叫

[istream removeFromRunLoop:[NSRunLoop currentRunLoop] 
        forMode: NSDefaultRunLoopMode]; 

您已經關閉了流之後。

0

發生了什麼是CameraViewer類(它被設置爲委託)的任何實例正在被釋放(導致運行循環中出現EXC_BAD_ACCESS),因爲您沒有保留它。

的解決方案是呼籲CameraViewer類保留在實例化,就像這樣:

CameraViewer *cameraViewer = [[CameraViewer alloc] init]; 
[cameraViewer retain];