imageCache :: memory leak (PR #85)

Pull request
https://github.com/chafey/cornerstone/pull/85
This commit is contained in:
Leonardo Campos 2017-04-03 02:36:56 -03:00
parent c5e94136be
commit 0beda39efd

View File

@ -532,8 +532,6 @@ if(typeof cornerstone === 'undefined'){
// dictionary of imageId to cachedImage objects // dictionary of imageId to cachedImage objects
var imageCache = {}; var imageCache = {};
// dictionary of sharedCacheKeys to number of imageId's in cache with this shared cache key
var sharedCacheKeys = {};
// array of cachedImage objects // array of cachedImage objects
var cachedImages = []; var cachedImages = [];
@ -574,10 +572,10 @@ if(typeof cornerstone === 'undefined'){
// remove images as necessary // remove images as necessary
while(cacheSizeInBytes > maximumSizeInBytes) { while(cacheSizeInBytes > maximumSizeInBytes) {
var lastCachedImage = cachedImages[cachedImages.length - 1]; var lastCachedImage = cachedImages[cachedImages.length - 1];
cacheSizeInBytes -= lastCachedImage.sizeInBytes; var imageId = lastCachedImage.imageId;
delete imageCache[lastCachedImage.imageId];
lastCachedImage.imagePromise.reject(); removeImagePromise(imageId);
cachedImages.pop();
$(cornerstone).trigger('CornerstoneImageCachePromiseRemoved', {imageId: lastCachedImage.imageId}); $(cornerstone).trigger('CornerstoneImageCachePromiseRemoved', {imageId: lastCachedImage.imageId});
} }
@ -600,7 +598,6 @@ if(typeof cornerstone === 'undefined'){
var cachedImage = { var cachedImage = {
loaded : false, loaded : false,
imageId : imageId, imageId : imageId,
sharedCacheKey: undefined, // the sharedCacheKey for this imageId. undefined by default
imagePromise : imagePromise, imagePromise : imagePromise,
timeStamp : new Date(), timeStamp : new Date(),
sizeInBytes: 0 sizeInBytes: 0
@ -620,22 +617,9 @@ if(typeof cornerstone === 'undefined'){
throw "putImagePromise: image.sizeInBytes is not a number"; throw "putImagePromise: image.sizeInBytes is not a number";
} }
// If this image has a shared cache key, reference count it and only cachedImage.sizeInBytes = image.sizeInBytes;
// count the image size for the first one added with this sharedCacheKey cacheSizeInBytes += cachedImage.sizeInBytes;
if(image.sharedCacheKey) {
cachedImage.sizeInBytes = image.sizeInBytes;
cachedImage.sharedCacheKey = image.sharedCacheKey;
if(sharedCacheKeys[image.sharedCacheKey]) {
sharedCacheKeys[image.sharedCacheKey]++;
} else {
sharedCacheKeys[image.sharedCacheKey] = 1;
cacheSizeInBytes += cachedImage.sizeInBytes;
}
}
else {
cachedImage.sizeInBytes = image.sizeInBytes;
cacheSizeInBytes += cachedImage.sizeInBytes;
}
purgeCacheIfNecessary(); purgeCacheIfNecessary();
}); });
} }
@ -662,24 +646,14 @@ if(typeof cornerstone === 'undefined'){
if (cachedImage === undefined) { if (cachedImage === undefined) {
throw "removeImagePromise: imageId must not be undefined"; throw "removeImagePromise: imageId must not be undefined";
} }
cachedImage.imagePromise.reject();
cachedImages.splice( cachedImages.indexOf(cachedImage), 1); cachedImages.splice( cachedImages.indexOf(cachedImage), 1);
cacheSizeInBytes -= cachedImage.sizeInBytes;
// If this is using a sharedCacheKey, decrement the cache size only
// if it is the last imageId in the cache with this sharedCacheKey
if(cachedImage.sharedCacheKey) {
if(sharedCacheKeys[cachedImage.sharedCacheKey] === 1) {
cacheSizeInBytes -= cachedImage.sizeInBytes;
delete sharedCacheKeys[cachedImage.sharedCacheKey];
} else {
sharedCacheKeys[cachedImage.sharedCacheKey]--;
}
} else {
cacheSizeInBytes -= cachedImage.sizeInBytes;
}
delete imageCache[imageId];
decache(cachedImage.imagePromise, cachedImage.imageId); decache(cachedImage.imagePromise, cachedImage.imageId);
delete imageCache[imageId];
return cachedImage.imagePromise; return cachedImage.imagePromise;
} }
@ -692,23 +666,20 @@ if(typeof cornerstone === 'undefined'){
} }
function decache(imagePromise, imageId) { function decache(imagePromise, imageId) {
imagePromise.then(function(image) { imagePromise.then(function(image) {
if(image.decache) { if(image.decache) {
image.decache(); image.decache();
} }
imagePromise.reject(); }).always(function() {
delete imageCache[imageId]; delete imageCache[imageId];
}).always(function() { });
delete imageCache[imageId];
});
} }
function purgeCache() { function purgeCache() {
while (cachedImages.length > 0) { while (cachedImages.length > 0) {
var removedCachedImage = cachedImages.pop(); var removedCachedImage = cachedImages[0];
decache(removedCachedImage.imagePromise, removedCachedImage.imageId); removeImagePromise(removedCachedImage.imageId);
} }
cacheSizeInBytes = 0;
} }
function changeImageIdCacheSize(imageId, newCacheSize) { function changeImageIdCacheSize(imageId, newCacheSize) {