Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No clean way to dispose objects in a context #194

Open
GoogleCodeExporter opened this issue May 31, 2015 · 0 comments
Open

No clean way to dispose objects in a context #194

GoogleCodeExporter opened this issue May 31, 2015 · 0 comments

Comments

@GoogleCodeExporter
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant