[Pyrex] Re: Ugly reference counting problem ahead!

Florian Schulze florian.proff.schulze at gmx.net
Sat Jul 19 12:49:24 CEST 2003


The real problem which lead to this was really _PyString_Resize, so I added 
it to the test. I found another way to do it on the way, it seems to be the 
nicest solution. It generates very few C code and does correct reference 
counting.

test1 is the wrong version, test2 the ugly and test3 the current rather 
nice one.

Regards,
Florian

cdef extern from "Python.h":
    ctypedef extern struct PyObject

    PyObject *c_1PyString_FromStringAndSize "PyString_FromStringAndSize" 
(char *v, int len)
    object c_2PyString_FromStringAndSize "PyString_FromStringAndSize" (char 
*v, int len)
    int _PyString_Resize(PyObject **string, int len)
    void Py_DECREF(PyObject *)

def test1():
    # wrong
    cdef PyObject *rv

    rv = c_1PyString_FromStringAndSize(NULL, 200000)
    _PyString_Resize(&rv, 100000)
    return <object>rv

def test2():
    # ugly
    cdef PyObject *rv
    cdef object dummy

    rv = c_1PyString_FromStringAndSize(NULL, 200000)
    _PyString_Resize(&rv, 100000)
    dummy = <object>rv
    Py_DECREF(<PyObject *>dummy)
    return dummy

def test3():
    # best?
    cdef object rv
    cdef PyObject *dummy

    rv = c_2PyString_FromStringAndSize(NULL, 200000)
    dummy = <PyObject *>rv
    _PyString_Resize(&dummy, 100000)
    return rv

#printf("RefCount: %i\n", __pyx_r->ob_refcnt);

On Sat, 19 Jul 2003 00:47:07 +0200, Florian Schulze 
<florian.proff.schulze at gmx.net> wrote:

> Hi!
>
> I got a problem with self generated objects and reference counting. 
> Function test1 leaks memory and in test2 I fixed that, but it's ugly. Can 
> this be done easier? I use this in my own file like class and this is the 
> most efficent way I found. I can send I the full class on request. Or you 
> can look in python/Objects/fileobject.c in the file_read function. I 
> closely follow it.
>
> Regards,
> Florian Schulze





More information about the Pyrex mailing list