[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] bug, patch: surflock.c memory leak



Hi,

this patch/bug report is from Tom in bugzilla.
http://pygame.motherhamster.org/bugzilla/show_bug.cgi?id=30

I think it sounds sensible.

--------

This issue is in the surflock.c, not surface.c. I diagnosed it in 1.8.1, but a
quick look at subversion shows that the relevant code has not changed.

The PySurface_LockBy function is leaking a weakref.ref each time a surface is
locked. The weak reference is created on line 82 of surflock.c, and then is
appended to the list on line 90. PyList_Append does not steal the reference,
and so the reference count remains incremented even when PySurface_LockBy
returns, causing a memory leak.

The solution is to add Py_DECREF(ref) as line 91 of surflock.c, making the
function read:

static int
PySurface_LockBy (PyObject* surfobj, PyObject* lockobj)
{
    PyObject *ref;
    PySurfaceObject* surf = (PySurfaceObject*) surfobj;

    if (!surf->locklist)
    {
        surf->locklist = PyList_New (0);
        if (!surf->locklist)
            return 0;
    }
    ref = PyWeakref_NewRef (lockobj, NULL);
    if (!ref)
        return 0;
    if (ref == Py_None)
    {
        Py_DECREF (ref);
        return 0;
    }
    PyList_Append (surf->locklist, ref);
    Py_DECREF(ref);

    if (surf->subsurface)
        PySurface_Prep (surfobj);
    if (SDL_LockSurface (surf->surf) == -1)
    {
        PyErr_SetString (PyExc_RuntimeError, "error locking surface");
        return 0;
    }
    return 1;
}