扫描仪插件内存泄漏仅在 iOS 10
Memory leak in scanner plugin only in iOS 10
我有一个使用 phonegap-plugin-barcodescanner (v 6.0.1) 的 cordova(当前为 5.3.3)项目。最近我遇到了我认为是 iOS 10 中的某种大内存泄漏。当应用程序在 iOS 10 上运行时,它似乎永远不会释放与扫描器相关的资源。但是在 iOS 7 或 9 上,一切正常。这可以在下面的内存报告中看到。
iOS 10
iOS 7
我尝试使用 Instruments 中的泄漏检查器来追踪泄漏,但我似乎无法在测试中找到任何实质性内容。使用该工具我只能找到每次扫描大约 1KB 的泄漏对象。
我想我的主要问题是,是否有更好的方法来跟踪似乎与此扫描仪相关的内存问题?在 iOS 10 中发生的 deallocation/ref 计数方式是否有某种变化会导致如此剧烈的变化?
我已经使用上面提到的插件创建了一个准系统 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>
编辑:经过进一步调查,我注意到虚拟机中似乎有相当大的 IOKit 分配,我不确定它来自哪里。您可以从此处的 VM 摘要中看到,对于 iPad,它没有分配这 100 MiB 块,但在 iOS 10 中分配。我仍然不太确定这是从哪里来的,或者什么会迫使这些额外的电话。
iOS 10
iOS 7
我有同样的问题,但我现在解决了。只需将您的插件文件 CDVBarcodeScanner.mm 替换为以下 https://github.com/phonegap/phonegap-plugin-barcodescanner/blob/master/src/ios/CDVBarcodeScanner.mm
经过 github 的一些讨论和进一步调查,我发现问题是由使用 AVCaptureVideoDataOutputSampleBufferDelegate 的插件引起的。视频输出设置似乎导致了 IOKit 级别的一些泄漏。
对此的解决方案是更新插件以使用 AVCaptureMetadataOutputObjectsDelegate 并更新输出以使用 AVCaptureMetadataOutput 类型。这需要实现方法 (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection
此方法与上一个方法非常相似,但不是使用 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]];
}
这些提到的更改是作为插件 v 6.0.3 的一部分发布的。
我有一个使用 phonegap-plugin-barcodescanner (v 6.0.1) 的 cordova(当前为 5.3.3)项目。最近我遇到了我认为是 iOS 10 中的某种大内存泄漏。当应用程序在 iOS 10 上运行时,它似乎永远不会释放与扫描器相关的资源。但是在 iOS 7 或 9 上,一切正常。这可以在下面的内存报告中看到。
iOS 10
iOS 7
我尝试使用 Instruments 中的泄漏检查器来追踪泄漏,但我似乎无法在测试中找到任何实质性内容。使用该工具我只能找到每次扫描大约 1KB 的泄漏对象。
我想我的主要问题是,是否有更好的方法来跟踪似乎与此扫描仪相关的内存问题?在 iOS 10 中发生的 deallocation/ref 计数方式是否有某种变化会导致如此剧烈的变化?
我已经使用上面提到的插件创建了一个准系统 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>
编辑:经过进一步调查,我注意到虚拟机中似乎有相当大的 IOKit 分配,我不确定它来自哪里。您可以从此处的 VM 摘要中看到,对于 iPad,它没有分配这 100 MiB 块,但在 iOS 10 中分配。我仍然不太确定这是从哪里来的,或者什么会迫使这些额外的电话。
iOS 10
iOS 7
我有同样的问题,但我现在解决了。只需将您的插件文件 CDVBarcodeScanner.mm 替换为以下 https://github.com/phonegap/phonegap-plugin-barcodescanner/blob/master/src/ios/CDVBarcodeScanner.mm
经过 github 的一些讨论和进一步调查,我发现问题是由使用 AVCaptureVideoDataOutputSampleBufferDelegate 的插件引起的。视频输出设置似乎导致了 IOKit 级别的一些泄漏。
对此的解决方案是更新插件以使用 AVCaptureMetadataOutputObjectsDelegate 并更新输出以使用 AVCaptureMetadataOutput 类型。这需要实现方法 (void)captureOutput:(AVCaptureOutput*)captureOutput didOutputMetadataObjects:(NSArray*)metadataObjects fromConnection:(AVCaptureConnection*)connection
此方法与上一个方法非常相似,但不是使用 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]];
}
这些提到的更改是作为插件 v 6.0.3 的一部分发布的。