Where to clean up memory in Addons?
#1
Hello,

I want to write an Addon which interacts with a C library. To do this, I am using ctypes which seems to be the easiest way to do this.

Some memory is just kept as global variables in my C part and never exported to Python as it is not needed on the Python side.

To clean up the global stuff, I've just exported a "Cleanup" function from my C code. But what's the right place to call this, so it is called if the Addon is deactivated? Or do I don't have to care for this as the Addon keeps running anyway and is never really "stopped" until Kodi itself ends?
Reply
#2
Python is no different than C in this aspect - a script finishes when it reaches the last executable instruction. Special methods like long-running conditional loops are needed to keep your script running. After your script finishes, all global Python objects are garbage-collected (special cases aside). You can insert your "cleanup" function at the end of your executable code. Or, if you don't know exactly when your script will stop, you can wrap your C-exposed functions inside a class and create an instance of this class in your main script's global scope:

Code:
class Foo(object):
   # Some other stuff here

    def __del__(self):
       my_cleanup()

foo = Foo()

The __del__ method will be executed automatically when your foo object goes out of scope and is garbage-collected.
Reply
#3
(2016-09-13, 13:25)Roman_V_M Wrote: Or, if you don't know exactly when your script will stop, you can wrap your C-exposed functions inside a class and create an instance of this class in your main script's global scope:

The __del__ method will be executed automatically when your foo object goes out of scope and is garbage-collected.

Somewhat unrelated, but will the __del__ method be magically called if a script crashes? I'm suspecting not, but I have an addon that adds some skin properties that could use this if it does. When it crashes (which isn't often) you have to restart Kodi (or at least reload the skin) to clear those. If __del__ gets called even on a crash, that would be great.
Reply
#4
(2016-09-13, 16:11)pkscout Wrote:
(2016-09-13, 13:25)Roman_V_M Wrote: Or, if you don't know exactly when your script will stop, you can wrap your C-exposed functions inside a class and create an instance of this class in your main script's global scope:

The __del__ method will be executed automatically when your foo object goes out of scope and is garbage-collected.

Somewhat unrelated, but will the __del__ method be magically called if a script crashes? I'm suspecting not, but I have an addon that adds some skin properties that could use this if it does. When it crashes (which isn't often) you have to restart Kodi (or at least reload the skin) to clear those. If __del__ gets called even on a crash, that would be great.

If it is a "normal" crash because of an unhandled Python exception, then yes, the garbage-collector does fire up. You can try this and see Smile

Code:
class Foo(object):
    def __del__(self):
        print 'Foo destroyed'


foo = Foo()

assert False
Reply

Logout Mark Read Team Forum Stats Members Help
Where to clean up memory in Addons?0