Replies: 1 comment
-
Yes, you can also write from module_name import auto_release
auto_release(val, source_list=True) Here is the example without with statement, what do you think ? Codeimport obspython as obs
from contextlib import contextmanager, ExitStack
from itertools import cycle
datacycle = cycle([1, 2, 3, 4, 5])
class Example_leak:
def __init__(self, source_name=None):
self.source_name = source_name
def update_text(self):
source = obs.obs_get_source_by_name(self.source_name)
if source is not None:
data = str(next(datacycle))
settings = obs.obs_data_create()
obs.obs_data_set_string(settings, "text", data)
obs.obs_source_update(source, settings)
# 16:36:24.325: Number of memory leaks: 1543
# obs.obs_data_release(settings)
# obs.obs_source_release(source)
@contextmanager
def source_auto_release(source_name):
source = obs.obs_get_source_by_name(source_name)
try:
yield source
finally:
obs.obs_source_release(source)
@contextmanager
def data_auto_release():
settings = obs.obs_data_create()
try:
yield settings
finally:
obs.obs_data_release(settings)
class Example_with:
def __init__(self, source_name=None):
self.source_name = source_name
def update_text00(self):
with source_auto_release(self.source_name) as source:
if source is not None:
data = str(next(datacycle))
with data_auto_release() as settings:
obs.obs_data_set_string(settings, "text", data)
obs.obs_source_update(source, settings)
# 16:49:11.270: Number of memory leaks: 0
def update_text(self):
s = ExitStack()
source = s.enter_context(source_auto_release(self.source_name))
if source is not None:
data = str(next(datacycle))
settings = s.enter_context(data_auto_release())
obs.obs_data_set_string(settings, "text", data)
obs.obs_source_update(source, settings)
eg = Example_with() # class created ,obs part starts
def refresh_pressed(props, prop):
print("refresh pressed")
eg.update_text()
def script_description():
return "with statement example"
def script_update(settings):
eg.source_name = obs.obs_data_get_string(settings, "source")
obs.timer_remove(eg.update_text)
if eg.source_name != "":
obs.timer_add(eg.update_text, 16) # 16 ms
def script_properties(): # ui
props = obs.obs_properties_create()
p = obs.obs_properties_add_list(
props,
"source",
"Text Source",
obs.OBS_COMBO_TYPE_EDITABLE,
obs.OBS_COMBO_FORMAT_STRING,
)
sources = obs.obs_enum_sources()
if sources is not None:
for source in sources:
source_id = obs.obs_source_get_unversioned_id(source)
if source_id == "text_gdiplus" or source_id == "text_ft2_source":
name = obs.obs_source_get_name(source)
obs.obs_property_list_add_string(p, name, name)
obs.source_list_release(sources)
obs.obs_properties_add_button(props, "button", "Refresh", refresh_pressed)
return props |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
As you, @upgradeQ suggested to me, I started using
@contextmanager
functions to auto-release elements. But now, I see that I use that functions in 2 different ways:with
andstack
. This is part of the code and then, the complete file:Do they do the same? Is one better than the other? Can I unify and use only one method?
Full code
Beta Was this translation helpful? Give feedback.
All reactions