cython malloc-olt memória felszabadítása python-ból

Sziasztok!

Használnék egy python könyvtárat ami mögött cython alapú malloc alapú memóriaallokációt végző kód van:

https://gitlab.com/radish/PyV4L2Camera/-/blob/master/PyV4L2Camera/camer…

Az így lefoglalt memóriát átadja a "sima" python kódomnak:

https://gitlab.com/radish/PyV4L2Camera/-/blob/master/PyV4L2Camera/camer…

A visszaadott referenciát (fixme?) próbáltam 'del'-el felszabadítani, de ettől eltekintve elleakel a picsába a memóriát.

Telepítettem pip-el a cython-t (a legújabbat 3.0.0.a10-et) amiben _elvileg_ van mód a stdlib free wrappelésére:

https://cython.readthedocs.io/en/latest/src/tutorial/memory_allocation…

Viszont ez nem működik

ModuleNotFoundError: No module named 'cython.cimports.libc.stdlib'

elhal.

Ez vajon miért lehet?

Ez alapján létezik/het ilyen include van is benne wrapper a free-re:
https://github.com/cython/cython/blob/3.0.0a10/Cython/Includes/libc/std…

Lehet nem így kell behúzni?

Esetleg ha valaki tud más megoldást a felszabadításra az is jó lehet.
Most kb. annyi ötletem van,  mocskolok cythonban egy deallokáló rutint...

Hozzászólások

Szerkesztve: 2022. 02. 06., v – 12:37

en ebbe a Pyv4lcamera classba irnam bele hogy a close method szabaditsa fel a memoriat. ha jol latom a sorrendet, akkor a v4l2_munmap utan de a v4l2_close elott kene.

A vegtelen ciklus is vegeter egyszer, csak kelloen eros hardver kell hozza!

Hát lehet mégis kellene valami python only megoldás, mert ez a cython (ahogy sejtettem) egy masszív szopóroller...

Kiforkoltam, megfixeltem majd:

pi@raspberrypi:~/brute_force2/sw $ pip3 install -e git+https://gitlab.com/martonmiklos/PyV4L2Camera.git@dealloc_buffer_on_close#egg=PyV4L2Camera
............
-Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -I/usr/include/python3.7m -c PyV4L2Camera/camera.c -o build/temp.linux-armv7l-3.7/PyV4L2Camera/camera.o
    arm-linux-gnueabihf-gcc: error: PyV4L2Camera/camera.c: Nincs ilyen fájl vagy könyvtár
    arm-linux-gnueabihf-gcc: fatal error: no input files
    compilation terminated.
    error: command 'arm-linux-gnueabihf-gcc' failed with exit status 1
    
    ----------------------------------------
  Rolling back uninstall of PyV4L2Camera
Command "/usr/bin/python3 -c "import setuptools, tokenize;__file__='/home/pi/brute_force2/sw/src/pyv4l2camera/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" develop --no-deps --user --prefix=" failed with error code 1 in /home/pi/brute_force2/sw/src/pyv4l2camera/

 

camera.c WTF?

Ja a setup.py-nak van egy --use-cython argumentuma ami azt állítja, hogy c vagy pyx fájloakt fordítson. Tegyük defaulttá!

 

Eredmény:

pi@raspberrypi:~/brute_force2/sw $ pip3 install  git+https://gitlab.com/martonmiklos/PyV4L2Camera.git@dealloc_buffer_on_close
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Collecting git+https://gitlab.com/martonmiklos/PyV4L2Camera.git@dealloc_buffer_on_close
  Cloning https://gitlab.com/martonmiklos/PyV4L2Camera.git (to revision dealloc_buffer_on_close) to /tmp/pip-req-build-o9dudhah
Branch 'dealloc_buffer_on_close' set up to track remote branch 'dealloc_buffer_on_close' from 'origin'.
Switched to a new branch 'dealloc_buffer_on_close'
    Complete output from command python setup.py egg_info:
    Compiling PyV4L2Camera/camera.pyx because it changed.
    Compiling PyV4L2Camera/controls.pyx because it changed.
    [1/2] Cythonizing PyV4L2Camera/camera.pyx
    
    Error compiling Cython file:
    ------------------------------------------------------------
    ...
    from v4l2 cimport *
    ^
    ------------------------------------------------------------
    
    PyV4L2Camera/camera.pyx:1:0: 'PyV4L2Camera/camera' is not a valid module name
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-req-build-o9dudhah/setup.py", line 28, in <module>
        extensions = cythonize(extensions)
      File "/home/pi/.local/lib/python3.7/site-packages/Cython/Build/Dependencies.py", line 1117, in cythonize
        cythonize_one(*args)
      File "/home/pi/.local/lib/python3.7/site-packages/Cython/Build/Dependencies.py", line 1284, in cythonize_one
        raise CompileError(None, pyx_file)
    Cython.Compiler.Errors.CompileError: PyV4L2Camera/camera.pyx

(pip3 install v4l2 megvolt már)

def __dealloc__(self):

és itt gondoskodsz a felszabadításról.