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

Memory leak in surflock.c #30

Closed
illume opened this issue Aug 22, 2011 · 1 comment
Closed

Memory leak in surflock.c #30

illume opened this issue Aug 22, 2011 · 1 comment
Labels
bug surfarray pygame.surfarray

Comments

@illume
Copy link
Member

illume commented Aug 22, 2011

Originally reported by: René Dudfield (Bitbucket: illume, GitHub: illume)


== , 2009-04-14 12:31:07 -0700

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;
}

== illume, 2009-05-26 01:55:25 -0700

Thanks Tom!

Committed revision 2259.


@illume
Copy link
Member Author

illume commented Jan 11, 2017

Original comment by Thomas Kluyver (Bitbucket: takluyver, GitHub: takluyver):


Removing version: unspecified (automated comment)

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

No branches or pull requests

1 participant