Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/26.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
Ios AVCaptureSession放弃内存-分配-仪器_Ios_Objective C_Memory Management_Instruments_Avcapturesession - Fatal编程技术网

Ios AVCaptureSession放弃内存-分配-仪器

Ios AVCaptureSession放弃内存-分配-仪器,ios,objective-c,memory-management,instruments,avcapturesession,Ios,Objective C,Memory Management,Instruments,Avcapturesession,我使用默认的AVCaptureSession来捕获相机视图。 一切正常,我没有任何泄漏,但当我在启动和关闭AVCaptureDevice后使用分配查找废弃内存时,它显示了大约230个仍然活动的对象 这是我的密码: 控制器h: @interface Controller : UIViewController <AVCaptureVideoDataOutputSampleBufferDelegate> { AVCaptureSession *captureSession; AVCaptu

我使用默认的AVCaptureSession来捕获相机视图。
一切正常,我没有任何泄漏,但当我在启动和关闭AVCaptureDevice后使用分配查找废弃内存时,它显示了大约230个仍然活动的对象

这是我的密码:

控制器h:

@interface Controller : UIViewController <AVCaptureVideoDataOutputSampleBufferDelegate> {
AVCaptureSession *captureSession;
AVCaptureDevice *device;

IBOutlet UIView *previewLayer;
}
@property (nonatomic, retain) AVCaptureSession *captureSession;
@property (nonatomic, retain) UIView *previewLayer;

- (void)setupCaptureSession;
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer 
       fromConnection:(AVCaptureConnection *)connection;
- (UIImage *) imageFromSampleBuffer:(CMSampleBufferRef) sampleBuffer;
我用这个代码清理所有东西:

- (void)cancelTapped {
    [[self captureSession] stopRunning], self.captureSession = nil; 

    for (UIView *view in self.previewLayer.subviews) {
        [view removeFromSuperview];
    }

    [self dismissModalViewControllerAnimated:YES];
}

- (void)dealloc {
    [super dealloc];

    [captureSession release];
    [device release];
    [previewLayer release];
}
仪器向我展示了这样的东西:

你知道我做错了什么吗

这会泄露捕获会话,该会话将保持所有输入和输出以及所有内部助手的活动

两种选择:

AVCaptureSession *session = [[AVCaptureSession alloc] init];
self.captureSession = session;
[session release], session = nil;
// or:
self.captureSession = [[[AVCaptureSession alloc] init] autorelease];

super dealloc应该在其他释放之后调用,否则实例的内存可能不包含指向您正在释放的这些对象的有效指针,因此您不会释放它们,特别是当它们为零时。

但是它有什么问题吗?我在dealloc[captureSession release]中发布它;您声明了
@property(非原子,_retain_)AVCaptureSession*captureSession
所以这个setter保留了你新创建的对象,而这个对象(由于alloc/init)你已经拥有了。@danyowdee你能不能看一下这里的问题?你能看一下这里吗?
- (void)setupCaptureSession {       
   NSError *error = nil;

   [self setCaptureSession: [[AVCaptureSession alloc] init]];
   ...
AVCaptureSession *session = [[AVCaptureSession alloc] init];
self.captureSession = session;
[session release], session = nil;
// or:
self.captureSession = [[[AVCaptureSession alloc] init] autorelease];
- (void)dealloc {
    [super dealloc];

    [captureSession release];
    [device release];
    [previewLayer release];
}