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

Replaced __del__ with _cleanup to avoid assumptions about object state #120

Merged
merged 1 commit into from
Nov 5, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions python/neopixel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Adafruit NeoPixel library port to the rpi_ws281x library.
# Author: Tony DiCola ([email protected]), Jeremy Garff ([email protected])
import atexit

import _rpi_ws281x as ws


Expand Down Expand Up @@ -83,8 +85,18 @@ def __init__(self, num, pin, freq_hz=800000, dma=5, invert=False,

# Grab the led data array.
self._led_data = _LED_Data(self._channel, num)

# Substitute for __del__, traps an exit condition and cleans up properly
atexit.register(self._cleanup)

def __del__(self):
# Required because Python will complain about memory leaks
# However there's no guarantee that "ws" will even be set
# when the __del__ method for this class is reached.
if ws != None:
self._cleanup()

def _cleanup(self):
# Clean up memory used by the library when not needed anymore.
if self._leds is not None:
ws.ws2811_fini(self._leds)
Expand Down