Scripting filters example - destroy source in the right way (?)

Suslik V

Active Member
In the example:
there is code:
Lua:
...

  -- Calls the destroy function if the effect was not compiled properly
  if data.effect == nil then
    obs.blog(obs.LOG_ERROR, "Effect compilation failed for " .. effect_file_path)
    source_info.destroy(data)    -- FOR WHAT REASON?
    return nil
  end

...

-- Destroys and release resources linked to the custom data
source_info.destroy = function(data)
  if data.effect ~= nil then
    obs.obs_enter_graphics()
    obs.gs_effect_destroy(data.effect)
    data.effect = nil    -- IS IT NEEDED AT ALL?
    obs.obs_leave_graphics()
  end
end

...

Questions:
  1. What source_info.destroy(data) is doing here while there is null check present in the destroy itself if data.effect ~= nil ?
  2. Does the data.effect = nil required in the destroy (for this script and in general)?
 

khaver

Member
"~=" is "not equal". So if data.effect is not nil then enter graphics context, destroy filter from GPU core, remove data.effect reference from memory (= nil), leave graphics context.

This is just a guess on my part.
 

Suslik V

Active Member
Look, I'm using nulls for pointers when I need to make them invalid. I do it when the pointer can be used later (or concurrently) in the code. The contents of the pointer is not important for me until I will use it. But if I never use it (after the destroy), why I need to update it?
To clarify the question #2 - where in OBS (the code line or logic) this not null-ed effect (after the destroy) can cause harm?
---------------------------

The faulty shader program doesn't cancels the filter creation, the object (filter source) is still listed in the filters list with no properties available. So, why to call the destroy if it frees nothing (the effect wasn't allocated and double null check present) and destroys nothing (the filter is still on the list). I see that when I remove the filter from the list manually - the overriding method of destroy (from the script) is called. But that's all I see, nothing else.
To clarify question #1 - The example code has the line (call to source_info.destroy(data)) that doing nothing useful in my opinion. Can you explain why it is important there?
---------------------------

Probably I don't get the code right. Maybe this is obvious for pro, and this is some general rule or way, but I'm not a programmer - just amateur. So, I'm asking again.
 
Top