This is my Bin
class:
namespace deepstream
{
class Bin : public Element
{
public:
Bin(const std::string& name)
: Element("bin", name) {}
~Bin()
{
gst_element_set_state(GST_ELEMENT(this->getGObject()), GST_STATE_NULL);
}
Bin& add(Element element)
{
auto res = elements.insert({ element.getName(), element });
if (!res.second)
throw std::runtime_error("Failed to insert Element " + element.getName());
if (!gst_bin_add(GST_BIN(this->getGObject()), GST_ELEMENT(res.first->second.getGObject())))
{
elements.erase(res.first);
}
std::cout << "Add Element ... " << element.getName() << " to Bin " << this->getName() << std::endl;
return *this;
};
private:
std::map<std::string, Element> elements; ///< Map of element instances.
};
} // namespace deepstream
And this is my main.cpp
:
int main()
{
Pipeline pipeline("test_pipeline");
{
Bin bin("test_bin");
Element elem("queue", "queue");
std::cout << "Before add Element ref counter: " << GST_OBJECT_REFCOUNT_VALUE(elem.getGObject()) << std::endl;
bin.add(elem);
std::cout << "After add Element ref counter: " << GST_OBJECT_REFCOUNT_VALUE(elem.getGObject()) << std::endl;
}
return 0;
}
When I run the code, I get this error when exiting the scope where the Bin
is defined:
GStreamer-CRITICAL **: 10:15:12.559:
Trying to dispose object “queue”, but it still has a parent “test_bin”.
You need to let the parent manage the object instead of unreffing the object directly
Can you please explain how to fix this error?
Moreover, I noticed that if I print the reference counter value before and after adding the Element
to the Bin
, I get:
Before add Element ref counter: 1
Add Element … queue to Bin test_bin
After add Element ref counter: 2
While if I use the Pipeline
like this:
int main()
{
Pipeline pipeline("test_pipeline");
{
Element elem("queue", "queue");
std::cout << "Before add Element ref counter: " << GST_OBJECT_REFCOUNT_VALUE(elem.getGObject()) << std::endl;
pipeline.add(elem);
std::cout << "After add Element ref counter: " << GST_OBJECT_REFCOUNT_VALUE(elem.getGObject()) << std::endl;
}
return 0;
}
I get:
Before add Element ref counter: 1
Add Element … queue
After add Element ref counter: 3
Could you explain why this is happening? Additionally, could you describe how elements are added to the Pipeline
?
Thank you.