简体   繁体   English

仅在iOS 10中扫描程序插件中的内存泄漏

[英]Memory leak in scanner plugin only in iOS 10

I have a cordova(currently 5.3.3) project that uses the phonegap-plugin-barcodescanner (v 6.0.1). 我有一个使用phonegap-plugin-barcodescanner (v 6.0.1)的cordova(目前为5.​​3.3)项目。 Recently I have encountered what I believe is a large memory leak of some kind in iOS 10. When the application runs on iOS 10, it appears to never deallocate the resources tied to the scanner. 最近我遇到了我认为在iOS 10中存在某种大内存泄漏的情况。当应用程序在iOS 10上运行时,它似乎永远不会释放与扫描程序相关的资源。 However on iOS 7 or 9, everything works fine. 但是在iOS 7或9上,一切正常。 This can be seen below in the memory reports. 这可以在内存报告中看到。

iOS 10 iOS 10 ios10

iOS 7 IOS 7 ios7

I've attempted to trace the leaks using the leak checker in Instruments, but I cannot seem to find anything substantial in the tests. 我试图使用仪器中的泄漏检查器来追踪泄漏,但我似乎无法在测试中发现任何实质性内容。 All I can find using that tool is around 1KB of leaked objects per scan. 我使用该工具找到的每个扫描大约有1KB的泄漏对象。

I guess my main question would be, is there a better way to trace the memory issue that appears to be tied to this scanner? 我想我的主要问题是,是否有更好的方法来追踪似乎与此扫描仪相关的内存问题? Was there some kind of change in the way deallocation/ref counts are occurring in iOS 10 that would cause such a drastic change? 在iOS 10中发生重新分配/引用计数的方式是否会发生某种变化会导致如此剧烈的变化?

I've created a barebones cordova project with the above mentioned plugin, and implemented a basic scanner call into a button as shown in this code which should provide a reproducible test. 我用上面提到的插件创建了一个准系统cordova项目,并在一个按钮中实现了一个基本的扫描程序调用,如此代码所示,它应该提供可重现的测试。

<!DOCTYPE html>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->
<html>
<head>
    <!--
    Customize this policy to fit your own app's needs. For more guidance, see:
        https://github.com/apache/cordova-plugin-whitelist/blob/master/README.md#content-security-policy
    Some notes:
        * gap: is required only on iOS (when using UIWebView) and is needed for JS->native communication
        * https://ssl.gstatic.com is required only on Android and is needed for TalkBack to function properly
        * Disables use of inline scripts in order to mitigate risk of XSS vulnerabilities. To change this:
            * Enable inline JS: add 'unsafe-inline' to default-src
    -->

    <meta name="format-detection" content="telephone=no">
    <meta name="msapplication-tap-highlight" content="no">
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width">
    <link rel="stylesheet" type="text/css" href="css/index.css">
    <title>Hello World</title>
</head>
<body>
    <div class="app">
        <script type="text/javascript">
            function scanTest(field) {
                try {
                    debugger
                    var scanner = window.cordova.require("phonegap-plugin-barcodescanner.BarcodeScanner");
                    scanner.scan(
                                 function (result) {
                                 if (document.getElementById(field)) {
                                 document.getElementById(field).innerHTML = result.text;
                                 } else {
                                 alert("ScannerCouldntIdentifyMessage");
                                 }
                                 });
                } catch (e) {
                    alert("ErrorDescription: " + e);
                }
            }
        </script>
        <h1>Apache Cordova</h1>
        <div id="deviceready" class="blink">
            <p class="event listening">Connecting to Device</p>
            <p class="event received">Device is Ready</p>
            <p id="scan">Test scan</p>
            <a href="#" onclick="scanTest('scan')" id="scantest">Scan</a>
        </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
</body>
</html>

EDIT: After further investigation, I've noticed that there appears to be a rather large IOKit allocation in the VM that i'm not sure where it's coming from. 编辑:经过进一步的调查,我注意到VM中似乎有一个相当大的IOKit分配,我不知道它来自哪里。 You can see here from the VM Summary that with the iPad it is not allocating these 100 MiB chunks, but does so in iOS 10. I'm still not really that sure where this is coming from or what would force these extra calls. 你可以在VM摘要中看到,iPad没有分配这100个MiB块,但是在iOS 10中也是这样。我仍然不确定它来自何处或是什么会强制这些额外的呼叫。

iOS 10 iOS 10 ios10

iOS 7 IOS 7 ios7

I had same issue but i resolved it now. 我有同样的问题,但我现在解决了。 just replace your plugin file CDVBarcodeScanner.mm with following https://github.com/phonegap/phonegap-plugin-barcodescanner/blob/master/src/ios/CDVBarcodeScanner.mm 只需将您的插件文件CDVBarcodeScanner.mm替换为以下https://github.com/phonegap/phonegap-plugin-barcodescanner/blob/master/src/ios/CDVBarcodeScanner.mm

After some discussion on github and further investigation, I found that the issue was caused by the plugin using the AVCaptureVideoDataOutputSampleBufferDelegate. 在对github和进一步调查进行一些讨论后,我发现该问题是由使用AVCaptureVideoDataOutputSampleBufferDelegate的插件引起的。 It appeared that the video output settings were resulting in some leaks at the IOKit level. 看来视频输出设置导致IOKit级别的一些泄漏。

The resolution to this was to update the plugin to use the AVCaptureMetadataOutputObjectsDelegate and update the output to use the AVCaptureMetadataOutput type. 解决方法是更新插件以使用AVCaptureMetadataOutputObjectsDelegate并更新输出以使用AVCaptureMetadataOutput类型。 This required implementation of the method (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection 这需要实现方法(void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection

This method was very similar to the previous one, but instead of using the zxing library to do the decoding, the decoding was done with a loop and AVMetadata classes: 此方法与前一个方法非常相似,但不是使用zxing库进行解码,而是使用循环和AVMetadata类完成解码:

  for (AVMetadataObject *metaData in metadataObjects) {
    AVMetadataMachineReadableCodeObject* code = (AVMetadataMachineReadableCodeObject*)
[self.previewLayer transformedMetadataObjectForMetadataObject:
(AVMetadataMachineReadableCodeObject*)metaData];

  if ([self checkResult:code.stringValue]) {
    [self barcodeScanSucceeded:code.stringValue format:[self formatStringFromMetadata:code]];
  }

These mentioned changes were released as part of v 6.0.3 of the plugin. 这些提到的更改是作为插件v 6.0.3的一部分发布的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM