Objective c VNSequenceRequestHandler上的EXC_错误访问

Objective c VNSequenceRequestHandler上的EXC_错误访问,objective-c,macos,avfoundation,metal,vision,Objective C,Macos,Avfoundation,Metal,Vision,下面的代码使用Vision和AVFoundation框架在macOS上的内置摄像头上启用人脸跟踪。在某些情况下,由于队列com.apple.VN.trackersCollectionManagementQueue(serial)上的工作线程上的EXC\u BAD\u访问(code=2)导致代码崩溃。只要没有检测到人脸,应用程序就可以正常工作,但一旦检测到人脸并试图通过该方法跟踪它,应用程序就会崩溃 [\u sequenceRequestHandler性能请求:请求onCVPixelBuffer:

下面的代码使用Vision和AVFoundation框架在macOS上的内置摄像头上启用人脸跟踪。在某些情况下,由于队列
com.apple.VN.trackersCollectionManagementQueue(serial)
上的工作线程上的
EXC\u BAD\u访问(code=2)
导致代码崩溃。只要没有检测到人脸,应用程序就可以正常工作,但一旦检测到人脸并试图通过该方法跟踪它,应用程序就会崩溃

[\u sequenceRequestHandler性能请求:请求onCVPixelBuffer:pixelBuffer方向:ExiForOrientation错误:&error]
该方法在AVCaptureVideoDataOutputSampleBufferDelegate方法内调用

-(void)captureOutput:(AVCaptureOutput*)输出didOutputSampleBuffer:(CMSampleBufferRef)SampleBufferfromConnection:(AVCaptureConnection*)连接)
据我所知,
EXC\u BAD\u ACCESS
表示无法访问内存,错误代码(2)
KERN\u PROTECTION\u FAILURE
表示指定的内存有效,但不允许所需的访问形式。一个(可能过时的)技术说明解释说,这是由于线程试图写入只读内存造成的。由此,我了解到问题不是由过早释放或内存损坏引起的,而是由跨线程的内存访问控制引起的

我相信问题是在更新后出现的。当在游戏模板项目(Metal、SceneKit和SpriteKit)的方案编辑器中选中调试可执行文件时,会发生崩溃,但在应用程序和文档应用程序模板中使用时不会崩溃。当在物理设备上适应iOS时,该代码也能正常工作。我试图通过尽可能多地删除代码来隔离问题,以下文件可以添加到任何模板中

头文件

#导入
#进口。正在崩溃的项目(在我的计算机上)现在可以从中查看和下载。我的电脑是2019 MB Pro 16

控制台输出

ErrorTest1(13661,0x107776e00)malloc:nano区域由于无法预先分配保留的vm空间而被放弃。
2020-12-24 09:48:35.709965+0100 ErrorTest1[13661:811227]已启用金属GPU帧捕获
2020-12-24 09:48:36.675326+0100错误测试1[13661:811227][plugin]AddInstanceForFactory:没有注册id为F8BB1C28-BAE8-11D6-9C31-00039315CD46的工厂
2020-12-24 09:48:36.707535+0100错误测试1[13661:811227][plugin]AddInstanceForFactory:没有注册id为30010C1C-93BF-11D8-8B5B-000A95AF9C6A的工厂
2020-12-24 09:48:36.845641+0100错误测试1[13661:811227][]CMIOHardware.cpp:379:CMIOObjectGetPropertyData错误:2003332927,失败
2020-12-24 09:48:38.717546+0100 ErrorTest1[13661:811794][logging persist]无法打开[02c344acea]第44580行的文件
2020-12-24 09:48:38.717648+0100错误测试1[13661:811794][logging persist]os_unix.c:44580:(0)打开(/var/db/DetachedSignatures)-未定义的错误:0
2020-12-24 09:48:38.778975+0100 ErrorTest1[13661:811761][Metal Compiler Warning]警告:编译成功:
程序来源:61:16:警告:未使用的变量“输入切片计数”
常数输入片计数=(输入特性通道+3)/4;
^
2020-12-24 09:48:38.779198+0100 ErrorTest1[13661:811812][Metal Compiler Warning]警告:编译成功,出现以下情况:
程序来源:121:24:警告:比较不同符号的整数:“int”和“const constant uint”(又名“const constant unsigned int”)
对于(int kd=0;kd
LLDB-bt

*线程#5,队列='com.apple.VN.trackerCollectionManagementQueue',停止原因=EXC#U错误访问(代码=2,地址=0x70000deb1ff8)
帧#0:0x000000010739db33 libsystem_pthread.dylib`uuu chkstk_darwin+55
帧#1:0x000000010739dafc libsystem_pthread.dylib`thread_start+20
帧#2:0x000000010724277b libMTLCapture.dylib`uuu lldb_unnamed_symbol2507$$libMTLCapture.dylib+585
帧#3:0x00007fff29f597be MPSNEURALN网络` \uUUU lldb\u未命名\u symbol4427$$MPSNEURALN网络+1907
帧#4:0x00007fff29f5a3c2 MPSNEURAL网络`\uuuuuuLLDB\u未命名\u symbol443
ErrorTest1(13661,0x107776e00) malloc: nano zone abandoned due to inability to preallocate reserved vm space.
2020-12-24 09:48:35.709965+0100 ErrorTest1[13661:811227] Metal GPU Frame Capture Enabled
2020-12-24 09:48:36.675326+0100 ErrorTest1[13661:811227] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x6030000b7b50> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2020-12-24 09:48:36.707535+0100 ErrorTest1[13661:811227] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x6030000bb5a0> 30010C1C-93BF-11D8-8B5B-000A95AF9C6A
2020-12-24 09:48:36.845641+0100 ErrorTest1[13661:811227] [] CMIOHardware.cpp:379:CMIOObjectGetPropertyData Error: 2003332927, failed
2020-12-24 09:48:38.717546+0100 ErrorTest1[13661:811794] [logging-persist] cannot open file at line 44580 of [02c344acea]
2020-12-24 09:48:38.717648+0100 ErrorTest1[13661:811794] [logging-persist] os_unix.c:44580: (0) open(/var/db/DetachedSignatures) - Undefined error: 0
2020-12-24 09:48:38.778975+0100 ErrorTest1[13661:811761] [Metal Compiler Warning] Warning: Compilation succeeded with: 

program_source:61:16: warning: unused variable 'input_slice_count'
    const uint input_slice_count = (INPUT_FEATURE_CHANNELS + 3) / 4;
               ^
2020-12-24 09:48:38.779198+0100 ErrorTest1[13661:811812] [Metal Compiler Warning] Warning: Compilation succeeded with: 

program_source:121:24: warning: comparison of integers of different signs: 'int' and 'const constant uint' (aka 'const constant unsigned int')
    for(int kd = 0; kd < params.inputFeatureChannels; kd++)  // _ID = 3, RGB
                    ~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020-12-24 09:48:38.779441+0100 ErrorTest1[13661:811838] [Metal Compiler Warning] Warning: Compilation succeeded with: 

program_source:121:24: warning: comparison of integers of different signs: 'int' and 'const constant uint' (aka 'const constant unsigned int')
    for(int kd = 0; kd < params.inputFeatureChannels; kd++)  // _ID = 3, RGB
                    ~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020-12-24 09:48:39.072518+0100 ErrorTest1[13661:811838] [Metal Compiler Warning] Warning: Compilation succeeded with: 

program_source:61:16: warning: unused variable 'input_slice_count'
    const uint input_slice_count = (INPUT_FEATURE_CHANNELS + 3) / 4;
               ^
2020-12-24 09:48:39.073210+0100 ErrorTest1[13661:811842] [Metal Compiler Warning] Warning: Compilation succeeded with: 

program_source:98:16: warning: unused variable 'fm_group'
    const uint fm_group = threadgroup_id.z - splitId * params.simdsPerGroupData;
               ^
program_source:121:24: warning: comparison of integers of different signs: 'int' and 'const constant uint' (aka 'const constant unsigned int')
    for(int kd = 0; kd < params.inputFeatureChannels; kd++)  // _ID = 3, RGB
                    ~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
2020-12-24 09:48:39.073538+0100 ErrorTest1[13661:811812] [Metal Compiler Warning] Warning: Compilation succeeded with: 

program_source:98:16: warning: unused variable 'fm_group'
    const uint fm_group = threadgroup_id.z - splitId * params.simdsPerGroupData;
               ^
program_source:121:24: warning: comparison of integers of different signs: 'int' and 'const constant uint' (aka 'const constant unsigned int')
    for(int kd = 0; kd < params.inputFeatureChannels; kd++)  // _ID = 3, RGB
                    ~~ ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
* thread #5, queue = 'com.apple.VN.trackersCollectionManagementQueue', stop reason = EXC_BAD_ACCESS (code=2, address=0x70000deb1ff8)
    frame #0: 0x000000010739db33 libsystem_pthread.dylib`___chkstk_darwin + 55
    frame #1: 0x000000010739dafc libsystem_pthread.dylib`thread_start + 20
    frame #2: 0x000000010724277b libMTLCapture.dylib`___lldb_unnamed_symbol2507$$libMTLCapture.dylib + 585
    frame #3: 0x00007fff29f597be MPSNeuralNetwork`___lldb_unnamed_symbol4427$$MPSNeuralNetwork + 1907
    frame #4: 0x00007fff29f5a3c2 MPSNeuralNetwork`___lldb_unnamed_symbol4432$$MPSNeuralNetwork + 756
    frame #5: 0x00007fff29f5aa39 MPSNeuralNetwork`___lldb_unnamed_symbol4435$$MPSNeuralNetwork + 83
    frame #6: 0x00007fff339e50e8 Espresso`Espresso::MPSEngine::mps_convolution_kernel::recreate_kernel() + 230
    frame #7: 0x00007fff339e3c95 Espresso`Espresso::MPSEngine::convolution_kernel_base<Espresso::generic_convolution_kernel>::set_biases(std::__1::shared_ptr<Espresso::blob<float, 1> >) + 455
    frame #8: 0x00007fff339e724b Espresso`Espresso::MPSEngine::convolution_kernel_proxy::set_biases(std::__1::shared_ptr<Espresso::blob<float, 1> >) + 103
    frame #9: 0x00007fff338b3a8f Espresso`Espresso::generic_convolution_kernel::set_biases(std::__1::shared_ptr<Espresso::blob<float, 1> >, std::__1::shared_ptr<Espresso::abstract_batch>) + 49
    frame #10: 0x00007fff338bdee1 Espresso`Espresso::load_network_layers_post_dispatch(std::__1::shared_ptr<Espresso::net> const&, std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> const&, std::__1::shared_ptr<Espresso::cpu_context_transfer_algo_t> const&, std::__1::shared_ptr<Espresso::net_info_ir_t> const&, bool, Espresso::network_shape const&, Espresso::compute_path, bool, std::__1::shared_ptr<Espresso::blob_storage_abstract> const&) + 5940
    frame #11: 0x00007fff338ba6ee Espresso`Espresso::load_network_layers_internal(std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::network_shape const&, std::__1::basic_istream<char, std::__1::char_traits<char> >*, Espresso::compute_path, bool, std::__1::shared_ptr<Espresso::blob_storage_abstract> const&) + 793
    frame #12: 0x00007fff338c9294 Espresso`Espresso::load_and_shape_network(std::__1::shared_ptr<Espresso::SerDes::generic_serdes_object> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::network_shape const&, Espresso::compute_path, std::__1::shared_ptr<Espresso::blob_storage_abstract> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) + 576
    frame #13: 0x00007fff338cb715 Espresso`Espresso::load_network(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, std::__1::shared_ptr<Espresso::abstract_context> const&, Espresso::compute_path, bool) + 2496
    frame #14: 0x00007fff33d9603c Espresso`EspressoLight::espresso_plan::add_network(char const*, espresso_storage_type_t) + 350
    frame #15: 0x00007fff33daa817 Espresso`espresso_plan_add_network + 294
    frame #16: 0x00007fff30479b9d Vision`+[VNEspressoHelpers createSingleNetworkPlanFromResourceName:usingProcessingDevice:lowPriorityMode:inputBlobNames:outputBlobNames:explicitNetworkLayersStorageType:espressoResources:error:] + 517
    frame #17: 0x00007fff3047992d Vision`+[VNEspressoHelpers createSingleNetworkPlanFromResourceName:usingProcessingDevice:lowPriorityMode:inputBlobNames:outputBlobNames:espressoResources:error:] + 151
    frame #18: 0x00007fff303ce123 Vision`-[VNRPNTrackerEspressoModelCacheManager espressoResourcesFromOptions:error:] + 417
    frame #19: 0x00007fff303ce8c8 Vision`-[VNObjectTrackerRevision2 initWithOptions:error:] + 262
    frame #20: 0x00007fff304152df Vision`__54-[VNTrackerManager _createTracker:type:options:error:]_block_invoke + 207
    frame #21: 0x00000001072fc0b0 libdispatch.dylib`_dispatch_client_callout + 8
    frame #22: 0x000000010730d3b2 libdispatch.dylib`_dispatch_lane_barrier_sync_invoke_and_complete + 135
    frame #23: 0x00007fff30414f01 Vision`-[VNTrackerManager _createTracker:type:options:error:] + 261
    frame #24: 0x00007fff30414b52 Vision`-[VNTrackerManager trackerWithOptions:error:] + 509
    frame #25: 0x00007fff304dda4a Vision`-[VNRequestPerformer trackerWithOptions:error:] + 85
    frame #26: 0x00007fff30343ac4 Vision`-[VNTrackingRequest internalPerformRevision:inContext:error:] + 436
    frame #27: 0x00007fff3037fb08 Vision`-[VNRequest performInContext:error:] + 885
    frame #28: 0x00007fff303cd9a1 Vision`VNExecuteBlock + 58
    frame #29: 0x00007fff304dd105 Vision`-[VNRequestPerformer _performOrderedRequests:inContext:error:] + 674
    frame #30: 0x00007fff304dd482 Vision`-[VNRequestPerformer performRequests:inContext:onBehalfOfRequest:error:] + 352
    frame #31: 0x00007fff304dd586 Vision`-[VNRequestPerformer performRequests:inContext:error:] + 60
    frame #32: 0x00007fff304cbf1a Vision`-[VNSequenceRequestHandler _performRequests:onImageBuffer:gatheredForensics:error:] + 293
    frame #33: 0x00007fff304cc122 Vision`-[VNSequenceRequestHandler performRequests:onCVPixelBuffer:orientation:gatheredForensics:error:] + 111
    frame #34: 0x00007fff304cc0aa Vision`-[VNSequenceRequestHandler performRequests:onCVPixelBuffer:orientation:error:] + 28
  * frame #35: 0x0000000106fc5a97 ErrorTest1`-[LSPVision captureOutput:didOutputSampleBuffer:fromConnection:](self=0x0000608000047c20, _cmd="captureOutput:didOutputSampleBuffer:fromConnection:", output=0x00006030000ce770, sampleBuffer=0x0000614000091240, connection=0x00006030000d0c30) at LSPVision.m:246:9
    frame #36: 0x00007fff3786b2e0 AVFCapture`__56-[AVCaptureVideoDataOutput_Tundra _render:sampleBuffer:]_block_invoke + 213
    frame #37: 0x00000001077ff3bb libclang_rt.asan_osx_dynamic.dylib`__wrap_dispatch_async_block_invoke + 203
    frame #38: 0x00000001072fae78 libdispatch.dylib`_dispatch_call_block_and_release + 12
    frame #39: 0x00000001072fc0b0 libdispatch.dylib`_dispatch_client_callout + 8
    frame #40: 0x00000001073036b7 libdispatch.dylib`_dispatch_lane_serial_drain + 776
    frame #41: 0x0000000107304594 libdispatch.dylib`_dispatch_lane_invoke + 449
    frame #42: 0x0000000107312217 libdispatch.dylib`_dispatch_workloop_worker_thread + 1675
    frame #43: 0x000000010739eb15 libsystem_pthread.dylib`_pthread_wqthread + 314
    frame #44: 0x000000010739dae3 libsystem_pthread.dylib`start_wqthread + 15
                    for (VNFaceObservation *faceObservation in results) {
                        //[self _setEyePositionsForFace:faceObservation];
                        //NSLog(@"seeing face");
                    }
        // SequenceRequesthandler results in 10-20% cpu utilization
        NSLog(@"aaa");
        [_sequenceRequestHandler performRequests:requests onCVPixelBuffer:pixelBuffer orientation:exifOrientation error:&error];
        NSLog(@"bbb");
            if (error != nil) {
                NSLog(@"Failed to perform FaceLandmarkRequest: %@", error);
            } else {
                NSLog(@"ok");
            }
- (IBAction)buttonAction:(id)sender {
    NSLog( @"Button" );
    self.v.captureFrame;
}
2020-12-27 09:14:54.147536+0200 MetalCaptureTest[11392:317094] aaa
2020-12-27 09:14:54.184167+0200 MetalCaptureTest[11392:317094] bbb
2020-12-27 09:14:54.268926+0200 MetalCaptureTest[11392:317094] ok
2020-12-27 09:14:54.269374+0200 MetalCaptureTest[11392:317094] aaa
2020-12-27 09:14:54.314135+0200 MetalCaptureTest[11392:316676] Button
2020-12-27 09:14:54.316025+0200 MetalCaptureTest[11392:317094] bbb
2020-12-27 09:14:54.393732+0200 MetalCaptureTest[11392:317094] ok
2020-12-27 09:14:54.394171+0200 MetalCaptureTest[11392:317094] aaa
2020-12-27 09:14:54.432979+0200 MetalCaptureTest[11392:317094] bbb
2020-12-27 09:14:54.496887+0200 MetalCaptureTest[11392:317094] ok
2020-12-27 09:14:54.497389+0200 MetalCaptureTest[11392:317094] aaa
2020-12-27 09:14:54.533118+0200 MetalCaptureTest[11392:317094] bbb
2020-12-27 09:14:54.614813+0200 MetalCaptureTest[11392:317094] ok
2020-12-27 09:14:54.615394+0200 MetalCaptureTest[11392:317094] aaa
2020-12-27 09:14:54.663343+0200 MetalCaptureTest[11392:317094] bbb
2020-12-27 09:14:54.747860+0200 MetalCaptureTest[11392:317094] ok
ErrorTest1(11743,0x10900ce00) malloc: nano zone abandoned due to inability to preallocate reserved vm space.
2020-12-27 10:55:10.445333+0200 ErrorTest1[11743:344803] Metal GPU Frame Capture Enabled
2020-12-27 10:55:10.471650+0200 ErrorTest1[11743:344803] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x6030000aabc0> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2020-12-27 10:55:10.528628+0200 ErrorTest1[11743:344803] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x6030000ae130> 30010C1C-93BF-11D8-8B5B-000A95AF9C6A
2020-12-27 10:55:10.608753+0200 ErrorTest1[11743:344803] [] CMIOHardware.cpp:379:CMIOObjectGetPropertyData Error: 2003332927, failed
2020-12-27 10:55:11.408594+0200 ErrorTest1[11743:344873] [logging-persist] cannot open file at line 44580 of [02c344acea]
2020-12-27 10:55:11.408806+0200 ErrorTest1[11743:344873] [logging-persist] os_unix.c:44580: (0) open(/var/db/DetachedSignatures) - Undefined error: 0
2020-12-27 10:55:17.637382+0200 ErrorTest1[11743:344803] seeing face
2020-12-27 10:55:17.838354+0200 ErrorTest1[11743:344803] seeing face
2020-12-27 10:55:17.987583+0200 ErrorTest1[11743:344803] seeing face
2020-12-27 10:55:18.171168+0200 ErrorTest1[11743:344803] seeing face
2020-12-27 10:55:18.320957+0200 ErrorTest1[11743:344803] seeing face