openscenegraph / OpenSceneGraph

OpenSceneGraph git repository
http://www.openscenegraph.org
Other
3.25k stars 1.42k forks source link

Node is never deleted #1117

Closed florent-lamiraux closed 2 years ago

florent-lamiraux commented 2 years ago

I am using OpenSceneGraph as a framework for a GUI and I have some troubles with cached obj files. When I load several times the same file after modifying it between two loads, the object remains the same in the interface and I wonder why the object is not deleted when removed from the scene tree.

More precisely, I wonder why here https://github.com/florent-lamiraux/gepetto-viewer/blob/ec82cb0c80082c4c4c13195705aa2ccae3439216/src/leaf-node-collada.cpp#L111 I get the following output in the terminal:

leaf-node-collada.cpp:111: reference count=2

when I expect to get reference count=1. Note that I use version 3.6.5 of OpenSceneGraph.

robertosfield commented 2 years ago

Debugging 3rd party code really isn't the job of the OSG community.

You are the one best placed to step through your code in a debugger and see what might be going. The best thing I can recommend is pay close attention to any caching that might be going, or any possible circular references that have been created.

florent-lamiraux commented 2 years ago

I do not ask you to debug my code of course, simply to tell me whether it is normal that upon calling

collada_ptr_ = osgDB::readNodeFile(collada_file_path_,options);

the number of reference count of collada_ptr_ is 2 instead of 1.

robertosfield commented 2 years ago

If you have the object cache enabled a reference will be taken by the ObjectCache.

florent-lamiraux commented 2 years ago

Thank you.

florent-lamiraux commented 2 years ago

For your information, I found the bug:

ObjectCache object_cache;
osg::ref_ptr<osgDB::Options>  options = new osgDB::Options();
options->setObjectCache (object_cache);
options->setObjectCacheHint(osgDB::Options::CACHE_ALL);
::osg::NodeRefPtr collada_ptr = osgDB::readNodeFile(collada_file_path_,options);

and later, I removed the object from the cache without providing options

object_cache->removeFromObjectCache(collada_file_path_);

and in this case, the object is not removed from the cache. I do not know whether it is the expected behaviour, but I think it is worth telling you.

robertosfield commented 2 years ago

It's possible load files with different options so they end up with different sub graphs for them so the ObjectCache has to pair the loaded model with the filename and options. You need to provide this same pairing when deleting.

florent-lamiraux commented 2 years ago

Then, would it make sense not to allow to call removeFromObjectCache without parameter option ?

robertosfield commented 2 years ago

On Mon, 31 Jan 2022 at 10:42, Florent Lamiraux @.***> wrote:

Then, would it make sense not to allow to call removeFromObjectCache without parameter option ? Message ID: @.*** com>

Maybe, but there's always a limit to how much you can second guess usage over every single class and method over the years that software has been developed but multiple developers.