Skip to content

Commit 668f106

Browse files
committed
Clean up object URL after extracting skin sprites
After sprites are extracted from the image via getSpriteUrisFromImg (which draws to canvas and creates data URLs), the original image and its object URL are no longer needed. Clean up the object URL at this point to prevent memory leaks. This only affects the fallback path (HTMLImageElement) since ImageBitmap doesn't have a src property and doesn't use object URLs.
1 parent 37b4e9c commit 668f106

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

packages/webamp/js/skinParserUtils.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,7 @@ export async function getFileFromZip(
5656
}
5757

5858
function fallbackGetImgFromBlob(blob: Blob): Promise<HTMLImageElement> {
59-
// Note: We cannot revoke the object URL here because the returned image
60-
// element needs the URL to remain valid for its lifetime. This is an
61-
// acceptable small leak since skin images are loaded infrequently and
62-
// the primary path uses createImageBitmap which doesn't have this issue.
59+
// Create object URL that will be cleaned up after the image is used
6360
return Utils.imgFromUrl(URL.createObjectURL(blob));
6461
}
6562

@@ -129,7 +126,17 @@ export async function getSpriteUrisFromFilename(
129126
if (img == null) {
130127
return {};
131128
}
132-
return getSpriteUrisFromImg(img, SKIN_SPRITES[fileName]);
129+
130+
// Extract sprites from the image
131+
const sprites = getSpriteUrisFromImg(img, SKIN_SPRITES[fileName]);
132+
133+
// Clean up object URL if the image is an HTMLImageElement with a blob URL
134+
// (ImageBitmap doesn't have a src property, so this only affects the fallback path)
135+
if (img instanceof HTMLImageElement && img.src.startsWith("blob:")) {
136+
URL.revokeObjectURL(img.src);
137+
}
138+
139+
return sprites;
133140
}
134141

135142
// https://docs.microsoft.com/en-us/windows/win32/xaudio2/resource-interchange-file-format--riff-

0 commit comments

Comments
 (0)