[C++-sig] Pyste: Virtual class wrapper doesn't refcount object

Dan Halbert halbert at bbn.com
Thu Apr 1 23:32:02 CEST 2004


In Pyste from the 1.31.0 release:

If Pyste finds any virtual methods in a class it is wrapping (if I
understand correctly), it generates a class wrapper for that class
that looks something like this:


    struct VMClass_Wrapper : VMClass_Wrapper
    {
        (PyObject* self_):
            VMClass(), self(self_) {}

        void f(long int p0) {
            call_method< void >(self, "f", p0);
	}

        PyObject* self;
    };


Note that the self object is not properly reference-counted. It seems
to me it should be, since it's held by the struct, and can disappear
from scope in the Python code, and so would go out of existence.

I had just such an error when using this class in a callback situation:

   registerMyCallback(VMClass(), ...)  # uses an anonymous VMClass() object


I fixed this by modifying the code generated by Pyste to use handle<>
and borrowed():

    struct VMClass_Wrapper : VMClass_Wrapper
    {
        (PyObject* self_):
            VMClass(), self(borrowed(self_)) {}    // was   self(self_)

        void f(long int p0) {
            call_method< void >(self.get(), "f", p0);  // was just  self
	}

        handle<> self;		              // was   PyObject* self;
    };


Anything wrong with this? I believe I am using borrowed() properly
here, but I'm not 100% sure. If it's OK, it's a fix to make to Pyste.

Dan




More information about the Cplusplus-sig mailing list