Skip to content
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

prevent crashes during completion of callbacks on disposed SoundFiles #320

Merged
merged 2 commits into from
Sep 11, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/soundfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,11 @@ define(function (require) {
request.onload = function() {
if (request.status === 200) {
// on sucess loading file:
if (!self.panner) return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does the trick, but I wonder if there's a better way to check if the soundfile has been disposed. Or, better yet, to cancel any incomplete callbacks when the soundfile is disposed.

ac.decodeAudioData(request.response,
// success decoding buffer:
function(buff) {
if (!self.panner) return;
self.buffer = buff;
self.panner.inputChannels(buff.numberOfChannels);
if (callback) {
Expand All @@ -232,6 +234,7 @@ define(function (require) {
},
// error decoding buffer. "e" is undefined in Chrome 11/22/2015
function() {
if (!self.panner) return;
var err = new CustomError('decodeAudioData', errorTrace, self.url);
var msg = 'AudioContext error at decodeAudioData for ' + self.url;
if (errorCallback) {
Expand All @@ -245,6 +248,7 @@ define(function (require) {
}
// if request status != 200, it failed
else {
if (!self.panner) return;
var err = new CustomError('loadSound', errorTrace, self.url);
var msg = 'Unable to load ' + self.url + '. The request status was: ' +
request.status + ' (' + request.statusText + ')';
Expand Down Expand Up @@ -276,7 +280,9 @@ define(function (require) {
else if (this.file !== undefined) {
var reader = new FileReader();
reader.onload = function() {
if (!self.panner) return;
ac.decodeAudioData(reader.result, function(buff) {
if (!self.panner) return;
self.buffer = buff;
self.panner.inputChannels(buff.numberOfChannels);
if (callback) {
Expand All @@ -285,6 +291,7 @@ define(function (require) {
});
};
reader.onerror = function(e) {
if (!self.panner) return;
if (onerror) {
onerror(e);
}
Expand Down Expand Up @@ -1301,6 +1308,7 @@ define(function (require) {

// act on the result
offlineContext.oncomplete = function(e) {
if (!self.panner) return;
var filteredBuffer = e.renderedBuffer;
var bufferData = filteredBuffer.getChannelData(0);

Expand Down Expand Up @@ -1652,9 +1660,10 @@ define(function (require) {
* <div><code>
* var inp, button, mySound;
* var fileName = 'cool';
* function setup() {
* function preload() {
* mySound = loadSound('assets/doorbell.mp3');
*
* }
* function setup() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 sorry for the trouble here!

* btn = createButton('click to save file');
* btn.position(0, 0);
* btn.mouseClicked(handleMouseClick);
Expand Down