JSExport based classes not released in JSContext

Viewed 64

When I create a class using the JSExport protocol

@objc protocol Font_JSExports: JSExport {
    
    var name        : String { get }
}

class Font          : NSObject, Font_JSExports
{    
    var name        : String
    var atlas       : MTLTexture?

    init(name: String)
    {   
        super.init()
        atlas = loadTexture( name )
    }
    
    deinit {
        if let texture = atlas {
            texture.setPurgeableState(.empty)
            atlas = nil
        }
        print("freeing font", name)
    }
}

And pass this class to the javascript context via a getFont() function

    class func getFont(_ name: String) -> Font
    {
        let list = getFonts()
        for f in list {
            if f.name == name {
                return f
            }
        }
        return list[0]
    }

And get a reference to the class from javascript

class Test
{
    constructor()
    {
        this.font = System.getFont("Square")
    }
}

The deinit destructor of the font class will never get called when I delete the JSContext. How to pass object references to JavaScript which get deallocated when the JSContext gets destructed ?

0 Answers
Related