Memory leak using Pin (callback)



  • Hello,

    LOPY 1.7.9.b3.

    If you run this piece of code you will see that memory is decreasing ... in the end it stops with 'MemoryError: memory allocation failed' . Comparing to timer memory leak issue, I believe there is ALWAYS a LEAK when you are using function callbacks.

    import gc
    import utime
    from machine import Pin
    
    
    class Object:
        
        def __init__(self): 
            print ("Object INIT ", self)
            
    
        def start(self):    
            
            def _call(arg):
                print('PIN changed')
            
            self._analogPin=Pin('P15',mode=Pin.IN,pull=Pin.PULL_UP)    
            self._analogPin.callback(Pin.IRQ_FALLING , _call)
             
            
        def __del__(self):
            print ("Object DEL ", self)
    
        
        def stop(self):    
            self._analogPin.callback(Pin.IRQ_FALLING , None)
            del self._analogPin
            self._analogPin = None
            
    
    print("Start memory %s" %(gc.mem_free()))
    
    SLEEP = 0.1
    
    gc.enable()
    while True:
        obj = Object()
        obj.start()
        utime.sleep(SLEEP)
        obj.stop()
        del obj 
        obj = None
        
        gc.collect()
        print("\t\tmemory %s" %(gc.mem_free()))

Log in to reply
 

Pycom on Twitter