soft pwm make memory leak, add pthread_detach() to pwm_start() may fix it.
A Python module to control the GPIO on a Raspberry Pi
Brought to you by:
croston
soft pwm make memory leak, add pthread_detach() to pwm_start() may fix it.
pwm_start() codes after add pthread_detach():
void pwm_start(unsigned int gpio)
{
struct pwm *p;
if (((p = find_pwm(gpio)) == NULL) || p->running)
return;
p->running = 1;
if (pthread_create(&threads, NULL, pwm_thread, (void *)p) != 0)
{
// btc fixme - error
p->running = 0;
return;
}
pthread_detach(&threads);
}
test codes as below:
import time
import RPi.GPIO as GPIO
if name == 'main':
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
while True:
GPIO.setup(21, GPIO.OUT)
pwm = GPIO.PWM(21, 50)
pwm.start(0)
for i in range(0, 101, 2):
pwm.ChangeDutyCycle(i)
time.sleep(0.03)
for i in range(100, -1, -2):
pwm.ChangeDutyCycle(i)
time.sleep(0.03)
pwm.stop()
GPIO.cleanup(21)
time.sleep(1)
thank you.
Thanks so much mate, your suggestion works. Just a little tweak needed:
pthread_detach(threads);
So without the pointer!
Now I can stop my servos to prevent them from making noises while not moving as often as I like :)
p.s.: I had to change the code myself, compile it using "python setup.py build" and then replace the file _GPIO.cpython-35m-arm-linux-gnueabihf.so on my pi at /usr/lib/python3/dist-packages/RPi/
Cheers,
Kim
Last edit: Kim Wüstkamp 2017-10-28
I created a github repo (https://github.com/wuestkamp/raspberry-gpio-python) which solves this issue and describes how to solve this.
Fix added to source code library. Will be included in next release.