-
-
Notifications
You must be signed in to change notification settings - Fork 338
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CanvasRenderingContext2D.prototype.drawImage leaks memory #677
Comments
I will look into it. |
@NAllred91 can you share your modified version of |
Should look something like that: // Apply CanvasRenderingContext2D.drawImage monkey patch
var drawImage = CanvasRenderingContext2D.prototype.drawImage;
CanvasRenderingContext2D.prototype.drawImage = (function () {
// Methods to address the memory leaks problems in Safari
let temporaryImage, imageElement;
const BASE64_MARKER = ';base64,';
const objectURL = window.URL || window.webkitURL;
function convertDataURIToBlob(dataURI) {
// Validate input data
if (!dataURI) {
return;
}
// Convert image (in base64) to binary data
const base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
const base64 = dataURI.substring(base64Index);
const raw = window.atob(base64);
const rawLength = raw.length;
let array = new Uint8Array(new ArrayBuffer(rawLength));
for (let i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
// Create and return a new blob object using binary data
return new Blob([array], { type: 'image/jpeg' });
}
return function (arg) {
var args = Array.prototype.slice.call(arguments);
var context = this;
if (arg instanceof HTMLVideoElement && arg.render) {
arg.render.save(function (base64Image) {
// Destroy old image
if (temporaryImage) {
objectURL.revokeObjectURL(temporaryImage);
}
// Create a new image from binary data
var imageDataBlob = convertDataURIToBlob('data:image/jpg;base64,' + base64Image);
// Create a new object URL object
imageElement = imageElement || new Image();
temporaryImage = objectURL.createObjectURL(imageDataBlob);
imageElement.addEventListener('load', function () {
args.splice(0, 1, imageElement);
drawImage.apply(context, args);
});
// Set the new image
imageElement.src = temporaryImage;
});
} else {
return drawImage.apply(context, args);
}
};
})(); |
Fixed in 6.0.21 and merged on master future 8.0.0 |
This is how I'm currently patching Your changes look good, but I think the I also have other changes in my code to only create the
|
Love it, will apply and let you know. |
Initial fix is part of 6.0.21 already release, i will make CanvasRenderingContext2D.prototype.drawImage return Promise and apply some other changes you made. |
YOU MUST read first!
Please use Community Forum for general technical discussions and questions.
extra/renderer-and-libwebrtc-tests.js
file).Note: If the checkboxes above are not checked (which you do after the issue is posted), the issue will be closed, removing this checkbox will result in automatic closed issue.
Versions affected
Cordova version (e.g 7.1.0): 10
Cordova iOS version (e.g 5.1.0): 6.2.0
Plugin version (e.g 6.0.12): 6.0.20
iOS version (e.g 10.2): 13
Xcode version (e.g 11.1 - 11A1027): 12
WebRTC-adapter version (e.g. 7.4.0): latest
WebRTC Framework version (e.g. JSSip 3.1.2): latest
Description
WebKit leaks memory when changing the
src
property of an image withdata:image/png;base64,
strings.See https://bugs.webkit.org/show_bug.cgi?id=31253
webkit.org bug ticket references a stackoverflow post suggesting to convert the DataURI to a blob. This allows you to manually call
revokeObjectURL
to free up memory.See https://stackoverflow.com/a/38624675
I am using
drawImage
to continuously write a video stream to a canvas. With your patch ofdrawImage
my app will crash after about 10-15 minutes.I've overwritten your patch in my app using the suggestion in the stackoverflow answer and my app no longer crashes.
This is the code that I believe needs updated to use a
Blob
andcreateObjectURL
in order to prevent the memory leak.cordova-plugin-iosrtc/js/iosrtc.js
Lines 223 to 242 in 7179dd5
The text was updated successfully, but these errors were encountered: