NSStream 在读入“EXC_BAD_ACCESS”时释放iPhone SDK

发布于 2024-09-28 08:22:55 字数 3421 浏览 1 评论 0原文

我有一个 NSStreamDelegate 视图控制器,当在流式传输某些内容时从导航控制器弹出视图时,我遇到了“EXC_BAD_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

I have a view controller that is an NSStreamDelegate, I have a problem when the view is popped from the navigation controller while something is being streamed I get a "EXC_BAD_ACCESS" error. I have tried closing the stream, but it doesn't seem to stop it if there is a stream in progress. What is the proper way to handle this, can you delay the view from popping if something is being streamed?

#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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雄赳赳气昂昂 2024-10-05 08:22:55

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

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

在关闭流后调用。

I see that you call scheduleInRunLoop. In that case, when you don't need the stream, you should also call

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

after you've closed the stream.

吃→可爱长大的 2024-10-05 08:22:55

发生的情况是 CameraViewer 类的任何实例(设置为委托)被释放(导致运行循环中的 EXC_BAD_ACCESS),因为您没有保留它。

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

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

What is happening is whatever instance of the CameraViewer class (which is set to be the delegate) is being deallocated (causing EXC_BAD_ACCESS in the run loop) because you didn't retain it.

The solution is to either call retain on the CameraViewer class at instantiation, like so:

CameraViewer *cameraViewer = [[CameraViewer alloc] init];
[cameraViewer retain];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文