(Python) Memory management in scripts?

redynotredy

New Member
I don't usually write programs or scripts which require memory management, so I have been looking at some scripts which has code like obs_data_get_array and later on they release the array using obs_data_array_release. When do I have to release references? When I am doing properties references? Or when I'm doing scene references?
I'm also curious about how to use the bfree() function, I keep getting
"in method 'bfree', argument 1 of type 'void *'" when I try to release a reference to obs_frontend_get_scene_names() using bfree()
 
Unfortunately for script writers, the API documentation describes the C interface. Lua and Python versions sometimes differ in various ways.
As best I can tell from my uses:
  • If a function returns an OBS object (scene, source, ...), the API description will tell you if you need to release it, and what function to use to do the release. So obs_data_get_array and obs_data_array_release. Many of these objects are reference counted, and failure to do the release (or doing one that isn't needed) tend to make OBS crash, either immediately or on shutdown.
  • Allocated strings and array-of-strings aren't primitives in C, so if a function returns them, you need to call bfree to free the memory. But in Lua and Python strings and arrary-of-string are built-in datatypes, and they have garbage collection to clean up the storage. So you don't call bfree on them - the language runtime takes care of it.
  • There are a few functions in the C API that can't be (successfully) called from Lua or Python - things with pointer-to-pointer arguments and the like. Some, but not all, have wrapper functions that allow them to be called from Python or Lua.
 
Top