0

Behaviour of deleted DigitalInOut without deinit
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Behaviour of deleted DigitalInOut without deinit

by kevinjwalters on Sat Jan 09, 2021 1:08 pm

If a program calls do_something() below it sets D0 to be an output and go high. The d0 variable's scope is local to the function so what happens after that / when it's garbage collected? Note: deinit() has not been called. Will this return to an input value at some unpredictable point in the program? Does anything happen if the program completes or terminates with an exception? Can this code be used to set an output value which is guaranteed to remain set for the duration of the program?

Code: Select all | TOGGLE FULL SIZE
def do_something():
    d0 = DigitalInOut(board.D0)
    d0.direction = Direction.OUTPUT
    d0.value = True
    return

kevinjwalters
 
Posts: 836
Joined: Sun Oct 01, 2017 3:15 pm

Re: Behaviour of deleted DigitalInOut without deinit

by tannewt on Mon Jan 11, 2021 6:45 pm

I believe the pin will remain an output until the VM is reset. The DigitalInOut will be garbage collected at some point but it doesn't call deinit automatically when it is. You are leaking the pin essentially and code shouldn't be written like this. (Code run at collection time is called the finalizer. We could use it to deinit on collect.)

Do NOT consider this guaranteed and write code assuming the current behavior. It's currently undefined and may change at any time.

tannewt
 
Posts: 2034
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.