Close an ImageBitmap when its drawing work is finished
Close an ImageBitmap after the work that needs it has finished. Tie that cleanup to the bitmap's owner and account for drawing failures as well as success.
For a bitmap used only for one canvas draw, a try/finally around the draw can make cleanup explicit. Create the bitmap before entering that block, draw it inside try, and call bitmap.close() in finally. Handle a failure to create the bitmap separately because no bitmap exists to close in that case.
If another operation still needs the same bitmap, the first draw is not its final use. Assign cleanup to one owner and close the bitmap after its required uses finish. Do not hand a closed bitmap to the next job.
Test the failure path as well as the successful draw. The cleanup path should run when drawing throws after a bitmap has been created.
Source: MDN ImageBitmap.close().
I work on Morphio, and this note is part of the image-workflow guidance I maintain.
About the author
- I build Morphio, an online toolkit for working with image formats, including HEIC conversion. I share practical notes on image processing and web tools.
Comments
Join the discussionNo comments yet. Be the first to comment.