Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone NSStream在读取“时释放”;EXC“不良访问”;软件开发包_Iphone_Nsstream - Fatal编程技术网

Iphone NSStream在读取“时释放”;EXC“不良访问”;软件开发包

Iphone NSStream在读取“时释放”;EXC“不良访问”;软件开发包,iphone,nsstream,Iphone,Nsstream,我有一个视图控制器,它是NSStreamDelegate,当某些内容正在流式传输时从导航控制器弹出视图时,我遇到了问题,我得到了一个“EXC\u BAD\u ACCESS”错误。我已尝试关闭流,但如果有流正在进行,它似乎无法阻止它。处理此问题的正确方法是什么?如果正在流式传输某些内容,是否可以延迟视图弹出 #import "CameraViewer.h" @implementation CameraViewer @synthesize camService; @synthesize curre

我有一个视图控制器,它是NSStreamDelegate,当某些内容正在流式传输时从导航控制器弹出视图时,我遇到了问题,我得到了一个“EXC\u BAD\u ACCESS”错误。我已尝试关闭流,但如果有流正在进行,它似乎无法阻止它。处理此问题的正确方法是什么?如果正在流式传输某些内容,是否可以延迟视图弹出

#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

我看到您调用了
scheduleinrunlop
。在这种情况下,当您不需要流时,还应该调用

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

关闭流后。

正在发生的事情是CameraViewer类(设置为委托)的任何实例正在被释放(导致运行循环中的EXC\u BAD\u访问),因为您没有保留它

解决方案是在实例化时调用CameraViewer类上的retain,如下所示:

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