Menu

#111 soft pwm make memory leak, add pthread_detach() to pwm_start() may fix it.

Done
None
Critical
Patch
2018-10-21
2015-07-29
jifu
No

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.

Discussion

  • Kim Wüstkamp

    Kim Wüstkamp - 2017-10-28

    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
  • Kim Wüstkamp

    Kim Wüstkamp - 2018-10-17

    I created a github repo (https://github.com/wuestkamp/raspberry-gpio-python) which solves this issue and describes how to solve this.

     
  • Ben Croston

    Ben Croston - 2018-10-21

    Fix added to source code library. Will be included in next release.

     
  • Ben Croston

    Ben Croston - 2018-10-21
    • status: New --> Done
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.