AlkenePan / pyv8

Automatically exported from code.google.com/p/pyv8
0 stars 0 forks source link

No clean way to dispose objects in a context #194

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
Run this code:

import weakref
import gc

import PyV8

tracks = {} #map weakref id to (weakref, description)
def tracked_deleted(r):
    _, descr = tracks[id(r)]
    print ("Tracked object '%s' just deleted." % (descr,))
    del tracks[id(r)]
def track(o, descr=""):
    r = weakref.ref(o, tracked_deleted)
    tracks[id(r)] = (r, "%s:%s" % (o.__class__.__name__, descr))
    return o

class PyObj(object):
    def __init__(self, name):
        track(self, name)

class Global(object):
    def Obj(self, name):
        return PyObj(name)

    def out(self, v):
        print v, type(v)

def run_case(name, js, cleanup):
    print "\nCase %s:" % (name,)
    ctxt = track(PyV8.JSContext(Global()), 'context')

    with ctxt:
        ctxt.eval(js)

        print "  V8 cleanup"
        ctxt.eval(cleanup)
        print "  V8 gc"
        PyV8.JSEngine.collect()
        print "  Py gc"
        gc.collect()
        print "  V8 gc"
        PyV8.JSEngine.collect()
        print "  Py gc"
        gc.collect()
        print "  Py gc.garbage:", gc.garbage

#------------------

run_case("ctxt dispose test", """
    var obj = Obj('box');
""", """
""")

What is the expected output? What do you see instead?

Once `run_case` finishes running, there is no longer any possible way to refer 
to `Obj('box')`. Thus I'd expect it to eventually be garbage collected. Instead 
I have to do the following and then run a GC:

    for key in ctxt.locals.keys():
        ctxt.locals[key] = None

What version of the product are you using? On what operating system?

PyV8 r443, Python 2.6.6, Windows 7

Please provide any additional information below.

I'm not sure if this is a V8 issue or a PyV8 issue.. I notice a 
Context::dispose() method in the V8 docs but this isn't exposed in PyV8. Not 
sure whether that would solve my problem.

Original issue reported on code.google.com by csaft...@gmail.com on 23 Jul 2013 at 5:57