Context leak detected — some help?



  • Hi,
    I am currently working on a rather simple script what should end in an animation export. To boost things up I am running the final script in the Terminal, but right now I am stuck with an error: Context Leak detected, msgtracer returned -1 which gives me some insight why DrawBot kept crushing on this script. 😄

    For the setup:
    I want to generate an ImageObject() which will
    then be rotated, for that I wrote my own function

    def drawRotatedImage(img, angle, pos):
        x, y = pos
        oval(x - r, y - r, r * 2, r * 2)
        clipPath()
        rotate(angle, center = (x, y))
        image(img, (0, 0))
    

    I setup my own ImageObject

    im = ImageObject()
    with im:
        size("A3")
        # Drawing
    

    And then call the animation setup as follows:

    for frame in range(numFrames):
        newPage("A3")
        drawRotatedImage(im, angle, pos)
        # call the function bunch of times
    

    I narrowed it down that the problem lies somewhere within drawRotatedImage() and I think it has something to do with drawing the image object within this function. But at the moment I am lost on how I could fix this problem.

    Any help and explanation on this problem will be much appreciated! 🙂


  • admin

    Trying to connect every step in a simplified script script.
    This runs fine... what is your macOS version?

    w = h = 300
    r = w / 2
    
    def drawRotatedImage(img, angle, pos):
        x, y = pos
        fill(None)
        stroke(0)
        oval(x - r, y - r, r * 2, r * 2)
        clipPath()
        rotate(angle, center = (x, y))
        image(img, (0, 0))
    
    
    img = ImageObject()
    with img:
        size(w, h)
        fontSize(100)
        text("Heee", (10, 10))
    
    for a in range(0, 360, 30):
        newPage(w, h)
        drawRotatedImage(img, a, (w/2, h/2))
    


  • Hi @frederik,
    I am running macOS version 10.14.6 and the simplified script works perfectly fine on my machine as well.
    As I was trying to extend it I got the same output again and I did some trial and error.

    What am I trying to achieve?
    I have in mind a setup where I draw an ImageObject. The ImageObject then can contain text or graphics as well as another image. I then feed it into some arbitrary function to manipulate it.
    The clue is that I want to manipulate the manipulated image again. So I am currently returning a new ImageObeject from the manipulation function.

    I came up with something like this:

    w = h = 300
    # higher Numbers here result in the context leak
    numManipulations = 20
    
    def manipulateImage(img, r, angle, pos):
        tempImg = ImageObject()
        x, y = pos
    
        with tempImg:
            size(w, h)
            # Draw the old Image
            image(img, (0, 0))
            fill(None)
            stroke(0)
            # Draw the manipulation on top
            oval(x - r, y - r, r * 2, r * 2)
            clipPath()
            rotate(angle, center = (x, y))
            image(img, (0, 0))
            
        return(tempImg)
    
    im = ImageObject()
    
    with im:
        size(w, h)
        fill(1)
        rect(0, 0, w, h)
        fill(0)
        fontSize(100)
        text("Heee", (10, 10))
    
    for a in range(numManipulations):
        r = randint(20, 40)
        x = randint(r, w-r)
        y = randint(r, h-r)
        angle = randint(0, 360)
        im = manipulateImage(im, r, angle, (x, y))
        
        newPage(w, h)
        image(im, (0, 0))
    
        saveImage(f"~/Desktop/test/RotatingImage-{a}.png")    
        
    

    What I found out so far:

    • The scripts runs fine when you only save the final image.
      Nope, running 100 repetitions results in the same Context leak

    • When I am trying to save every step for animation purpose or so I will get Context leak deteced, msgtracer -1. No matter using gif, mp4 or png for later stitching.
      With png it usually starts at around 12 repetitions, mp4 or gif can go up to 100 repetitions, but I guess it depends on the resolution and other factors.

    • Running the script within the app you see no problem except on a lot of repetitions the app freezes at some point and you need to kill it. Running the same script then in Terminal you see the Context leak deteced, msgtracer -1.

    Maybe it is not the best setup for what I have in mind, but I hope one can understand what's happening.
    Any input for improvment is much appreciated as well. 🙂


  • admin

    try to do saveImage outside the loop with the option multiPage set to True.

    saveImage(f"~/Desktop/test/issueContextLeak/RotatingImage.png", multipage=True)    
    

    Somehow your image object got lost in between saveImage actions...

    see saveImage options: https://www.drawbot.com/content/canvas/saveImage.html#drawBot.saveImage


Log in to reply