drawbot icon indicating copy to clipboard operation
drawbot copied to clipboard

Objective-C reference counting error when applying filters.

Open typesupply opened this issue 7 months ago • 2 comments

FYI, I found an issue in PyObjC that pertains to DrawBot: https://github.com/ronaldoussoren/pyobjc/issues/649#issuecomment-2895651488

I first encountered this error in DrawBot before I moved to running from the commandline. This script will trigger the issue in DrawBot:

import drawBot as bot

steps = 20
step = 50
imageWidth = step * steps
imageHeight = imageWidth
imageRect = (0, 0, imageWidth, imageHeight)

sections = []
for x in range(steps):
    x *= step
    for y in range(steps):
        y *= step
        sections.append((x, y, step, step))

def sampleImage():
    image = bot.ImageObject()

    with image:
        bot.size(imageWidth, imageHeight)
        bot.fill(1, 1, 1, 1)
        bot.rect(*imageRect)
        bot.fill(0, 0, 0, 1)
        path = bot.BezierPath()
        path.oval(*imageRect)
        bot.drawPath(path)

    samples = {}

    for section in sections:
        i = image.copy()
        i.areaAverage(section)
        color = bot.imagePixelColor(i, (0, 0))
        samples[section] = color

    return image

for i in range(100):
    print(f"try {i}...")
    sampleImage()

As Ronald suggested, wrapping the for section in sections in with objc.autorelease_pool(): solves the problem. I don't know if you want to wrap the bitmap representation code in this or leave it up to the user.

typesupply avatar May 20 '25 20:05 typesupply