Tk Source Code

View Ticket
Login
Ticket UUID: 6872190648bdeabaf61322b9d571ec70334ce021
Title: Aqua: over-release in CreateCGImageFromDrawableRect()
Type: Patch Version: core-9-0-branch
Submitter: chrstphrchvz Created on: 2025-11-25 02:52:28
Subsystem: 82. X11 Emulation Assigned To: marc_culler
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-10-01 19:01:42
Resolution: Accepted Closed By: marc_culler
    Closed on: 2026-10-01 19:01:42
Description:

CreateCGImageFromDrawableRect() incorrectly releases the colorspace object returned by CGImageGetColorSpace(image). The code examples at https://web.archive.org/web/20200219030756/http://blog.foundry376.com/2008/07/scaling-a-cgimage/#comment-200 contain the same problem.

I only notice this issue now because the colorspace used recently changed with [cd84376c]. Some colorspace objects do not respond to a release message (CFGetRetainCount() always returns 4294967295). Other colorspace objects have a reference count of only about 100 when Tk launches, so after enough calls to CreateCGImageFromDrawableRect() (specifically when downscaling a Retina source drawable), Tk will crash.

User Comments: marc_culler (claiming to be Marc Culler) added on 2026-10-01 19:01:42:
And removing that call to CGColorSpaceRelease( eliminates the crash.

I have merged the fix into 9.0 and 9.1.

marc_culler (claiming to be Marc Culler) added on 2026-10-01 18:37:56:
Yup.  That crashes on my macbook Air M3.

serhiy.storchaka added on 2026-10-01 17:39:15:

Still present in 9.0 and 9.1 (8.6 does not have this code). The fix in branch [bug-6872190648] is correct: CGImageGetColorSpace() follows the Get rule, so its result must not be released. The branch merges cleanly into main. Could someone with macOS test and merge it into core-9-0-branch and main?

A probable reproducer (untested, needs a Retina display): [.c postscript] grabs the embedded label with XGetImage(), which takes the downscaling path.

pack [canvas .c -width 200 -height 100]
label .c.l -text Hello
.c create window 100 50 -window .c.l
update
for {set i 1} {$i <= 1000} {incr i} {
    .c postscript
}
puts OK