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

Calling python func on a python object from javascript permanently increases reference count, leading to memory leaks. #185

Open
GoogleCodeExporter opened this issue Mar 15, 2015 · 5 comments

Comments

@GoogleCodeExporter
Copy link

What steps will reproduce the problem?
1. Run the following code (also attached)

import sys

import PyV8


class ProxyObj(object):
    def __init__(self):
        pass

    def what(self):
        pass


class Global(object):
    def Obj(self):
        return ProxyObj()


def case1():
    ctxt = PyV8.JSContext(Global())
    with ctxt:
        obj = ctxt.eval("""function foo() {
            var theObj = Obj();
            return theObj;
        }
        var res = foo();
        res;
        """)
        print "Case 1:", sys.getrefcount(obj)


def case2():
    ctxt = PyV8.JSContext(Global())
    with ctxt:
        obj = ctxt.eval("""function foo() {
            var theObj = Obj();
            theObj.what();
            theObj.what();
            theObj.what();
            return theObj;
        }
        var res = foo();
        res;
        """)
        print "Case 2:", sys.getrefcount(obj)


case1()
case2()

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

I would expect:

Case 1: 3
Case 2: 3

Because there should be 3 references to the object: The local python var 'obj', 
the reference from V8, and the reference that 'getrefcount' has while it's 
running.

I instead see:

Case 1: 3
Case 2: 6

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

Using Python 2.6.6 with PyV8-1.0-preview-r443.win32-py2.6.exe on Windows 7

Please provide any additional information below.

Inserting a "PyV8.JSEngine.collect()" call doesn't fix the issue.


Original issue reported on code.google.com by csaft...@gmail.com on 17 Jul 2013 at 4:40

Attachments:

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

Note that the same happens if Global and ProxyObj inherit from PyV8.JSClass vs. 
from object. 

The same also happens without having the function `foo` involved. That is:

var theObj = Obj();
theObj.what();
theObj.what();
theObj.what();
theObj;


Original comment by csaft...@gmail.com on 17 Jul 2013 at 4:42

@GoogleCodeExporter
Copy link
Author

[deleted comment]

@GoogleCodeExporter
Copy link
Author

* If ProxyObj inherits from JSObject, same behavior. 

* The function doesn't have to be called. If this is Case 2 , you get the same 
output:

                        var theObj = Obj();
                        theObj.what;
                        theObj.what;
                        theObj.what;
                        theObj;

* If 'what' is a variable or a function/not an instance method, bug doesn't 
happen, i.e.:

class ProxyObj(object):
    def __init__(self):
        self.what = None

or

def f():
    pass

class ProxyObj(object):
    def __init__(self):
        self.what = f

* It also doesn't happen if I make a 'fake' instance method:

def f(self):
    pass

class ProxyObj(object):
    def __init__(self):
        self.what = lambda: f(self)

In this case the reference counts are both '4'. 

* No bug if it's a static method or classmethod:

class ProxyObj(object):
    def __init__(self):
        pass

    @staticmethod
    def what():
        pass

or

class ProxyObj(object):
    def __init__(self):
        pass

    @classmethod
    def what():
        pass

In both cases, both cases are '3'.

* Basically seems to be an instancemethod issue.


Original comment by csaft...@gmail.com on 17 Jul 2013 at 4:52

@GoogleCodeExporter
Copy link
Author

* @property is also not a problem, that is:

class ProxyObj(object):
    def __init__(self):
        pass

    @property
    def what(self):
        return 100

Doesn't cause the leak

Original comment by csaft...@gmail.com on 17 Jul 2013 at 5:48

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