Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@low-batt
Copy link
Contributor

@low-batt low-batt commented Dec 5, 2025

This commit will change NowPlayingInfoManager.constructImage to use CGImageSourceCreateThumbnailAtIndex instead of an NSImage constructor to reduce memory use.

Apple recommends using CGImageSourceCreateThumbnailAtIndex to minimize memory use when a full image is not required.


Description:

This commit will change NowPlayingInfoManager.constructImage to use
CGImageSourceCreateThumbnailAtIndex instead of an NSImage constructor to
reduce memory use.

Apple recommends using CGImageSourceCreateThumbnailAtIndex to minimize
memory use when a full image is not required.
@low-batt
Copy link
Contributor Author

low-batt commented Dec 6, 2025

This pull request partially addresses this comment from @httpstorm:

There's a rule that says: never trust any data you don't control. It could be from the network, user controlled or in this case an external library that may have bugs.

If the size is known, check it, before constructing the NSImage. Else allocate memory and start reading. If a certain safe size is exceeded, fail. If all is good, I think the NSImage can be constructed from the Data just read.

That comment is on issue #5803 where, thanks to a lot of investigative work by @httpstorm, we know that libmpv 0.38.0 incorrectly identified a track as an image (fixed in 0.40.0) causing IINA to attempt construct an image from the huge video file using NSImage(contentsOf:). An Instruments trace provided by @httpstorm showed that NSImage constructor results in a call to the NSData(contentsOfFile:) constructor causing an attempt to malloc enough contiguous memory to read in the entire file.

That problem is fixed by PR #5818, however some side issues were raised in the comments on #5803. The above comment is one of them. This PR addresses one place where IINA might be tricked into attempting to construct a NSImage from a huge file. The proposed fix uses CGImageSourceCreateThumbnailAtIndex instead of NSImage(contentsOf:). Using this method is recommended by Apple in the WWDC 2018 video iOS Memory Deep Dive.

The additional use of CGImage means this part of the Now Playing support should be refactored to maintain the image as a CGImage object until it must be converted to a NSImage object. I will address that in some future PR. This PR solves the problem at hand.

The other place where the NSImage(contentsOf:) constructor is used is in PlayerCore.createImage. That code is used when the IINA setting Show previews after taking screenshots is enabled. As that code is reading a screenshot image file that was just created by libmpv it is not as vulnerable to attempting to read a massive file into memory. However, that code also does not need a full image, only a thumbnail sized appropriately for IINA's screenshot preview. So this code too should be refactored to use CGImageSourceCreateThumbnailAtIndex. I will address this as well in some future PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants