Skip to content

Conversation

@jholveck
Copy link
Contributor

@jholveck jholveck commented Jan 4, 2026

Previously, we would release the image data provider, but not the image. We actually need to release the image, and not the data provider. This is based on the CoreFoundation "Create/Copy/Get" ownership rules: we own things that we allocate with functions with Create or Copy in their name, but not things that have Get in their name.

I suspect that the CGImage held a reference to its data provider, which stored the pixel data. When we freed the data provider, we released the pixel data, but not the CGImage structure.

Now, we're releasing the CGImage, and it releases the data provider.

Empirical testing while taking 10000 successive screenshots showed that, once steady state was reached, the previous version was leaking an average of about 467 bytes per screenshot on average. The new version still leaks about 148 bytes per screenshot, although that may not be related to macOS.

Also fixed a few function types, but those were harmless.

Changes proposed in this PR

It is very important to keep up to date tests and documentation.

  • Tests added/updated (N/A)
  • Documentation updated (N/A)

Is your code right?

  • ./check.sh passed

Previously, we would release the image data provider, but not the
image.  We actually need to release the image, and not the data
provider.  This is based on the CoreFoundation "Create/Copy/Get"
ownership rules: we own things that we allocate with functions with
Create or Copy in their name, but not things that have Get in their
name.

I suspect that the CGImage held a reference to its data provider,
which stored the pixel data.  When we freed the data provider, we
released the pixel data, but not the CGImage structure.

Now, we're releasing the CGImage, and it releases the data provider.

Empirical testing while taking 10000 successive screenshots showed
that, once steady state was reached, the previous version was leaking
an average of about 467 bytes per screenshot on average.  The new
version still leaks about 148 bytes per screenshot, although that may
not be related to macOS.

Also fixed a few function types, but those were harmless.
@BoboTiG
Copy link
Owner

BoboTiG commented Jan 4, 2026

Thanks @jholveck!

A line in the changelog might be interesting?

@BoboTiG BoboTiG merged commit f2c32c9 into BoboTiG:main Jan 4, 2026
21 checks passed
@jholveck jholveck deleted the feat-macos-fix-cfrelease branch January 4, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Investigate object ownership in macOS

2 participants