Lua memory management

halioris

New Member
The scripting guide talks about making sure you release memory you use and does a decent job of posting on individual API's if a release is needed after calling it. I have a few more general questions and I'll start with the first one that's been bugging me here.

If you get an enumerator to loop through, they all say you need to release the list which is fine. My question is when you loop through the items and then save one for use after you release the list, what is really happening? First, I thought that releasing the list would have killed the reference saved off and it would be useless but that is not the case. Then I was wondering if maybe GC hadn't happened so it was luck that it was still available but don't think that is true either. I used the function below to get a scene (really a source) reference for a scene given a name. I use that object after calling the function and make changes to it and it works. But now I'm wondering how I have this source object for a scene that I seemingly don't need to release.

Code:
function getSceneByName(sceneName)
    local returnScene = nil
    local scene_list = obs.obs_frontend_get_scenes()
    if scene_list ~= nil then
        for _, scene in ipairs(scene_list) do
            if obs.obs_source_get_name(scene) == sceneName then
                returnScene = scene
                break
            end
        end
        obs.source_list_release(scene_list)
    end
    -- return the source object for the scene
    return returnScene
end

local scene = getSceneByName("myscene")
local scene_object = obs.obs_scene_from_source(scene)
-- get all items in the scene to set bySource visible and everything else invisible
local items = obs.obs_scene_enum_items(scene_object)
if items ~= nil then
    for _, item in ipairs(items) do
        local source = obs.obs_sceneitem_get_source(item)
        if source ~= nil then
            if obs.obs_source_get_name(source) == "mySource" then
                obs.obs_sceneitem_set_visible(item, true)
            else
                obs.obs_sceneitem_set_visible(item, false)
            end
        end
    end
    obs.sceneitem_list_release(items)
end

So is this all ok? I get a reference to a scene with local scene = getSceneByName("myscene") but then that function release the list, and I would have thought presumably all the items (in this case scenes) in it. I then proceed to use that scene (really source) object after calling the function to get it's scene object, loop through all the scenes in it and make one of them visible. This all seems to work but I want to make sure I'm doing it right. What is that scene that I am getting back from my getSceneByName and do I really not ever need to release it?

There is also a reference in the scripting guide to "check the memory leak counter in your log file to make sure scripts you write aren’t leaking memory". How do you do that? When I look at the OBS Script log I do not see memory leak counters. Do I just need to call some function to print them?
 
The "Script log" you see from the script menu shows only script-related messages. Memory leaks are reported at the end of the full OBS log file when you exit from OBS (usually stored someplace like C:\Users\your-user-name\AppData\Roaming\obs-studio). Something like:
21:08:14.930: Number of memory leaks: 13
Alas, no help with WHAT has been leaked.

As you have found, the docs are pretty good about specifying what you need to release, but cases like this are ambiguous. My experience indicates that you usually don't need to release the sub-items; but check the leak count.

If you try to release something that you SHOULDN'T, crashes tend to ensue, which can be informative :)
 

halioris

New Member
The "Script log" you see from the script menu shows only script-related messages. Memory leaks are reported at the end of the full OBS log file when you exit from OBS (usually stored someplace like C:\Users\your-user-name\AppData\Roaming\obs-studio). Something like:
21:08:14.930: Number of memory leaks: 13
Alas, no help with WHAT has been leaked.

As you have found, the docs are pretty good about specifying what you need to release, but cases like this are ambiguous. My experience indicates that you usually don't need to release the sub-items; but check the leak count.

If you try to release something that you SHOULDN'T, crashes tend to ensue, which can be informative :)
very helpful, thanks John. Now I can't wait to get home tonight and try to look in those logs for the memory leak info.

Any thoughts on my specific scenario above? Specifically am I safe saving that source reference before releasing the list and then referencing it afterwards and should it be working and not needing to be release?
 
Top