Re: [osg-users] How to delete osg graphs properly?
On 18/05/10 22:55 , Ku Krapox wrote: > particular I've learnt by heart the part on memory management. :P But the > problem is, > I'm doing everything right... All my objects are now in ref_ptr and I still > have memory > leaks. Maybe you've created a circular reference? Like two objects holding ref_ptrs to each other? /ulrich ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
Hi Ku, I had an idea : might inheritance interfere with the osg::Referenced system? I mean, if I create my own class derived from PAT, then put it in a ref_ptr, will it be handled correctly? No, it should work well as long as you follow the rules... see below. 'cause I've found out the following behaviour : class CustomPAT : public osg::PositionAttitudeTransform {...} int main() { PositionAttitudeTransform* myPAT1 = new PositionAttitudeTransform(); delete myPAT1; // Compilation error -> normal CustomPAT* myPAT2 = new CustomPAT(); delete myPAT2; // Compilation OK !!! -> shouldn't be } Your CustomPAT class probably does not declare a destructor. This means the compiler generates one for you, and it will be public, which is why you can delete myPAT2. Create a destructor and make it protected, even if it doesn't do anything. This is important, otherwise as you saw users can do things that are not good... I don't know what to think about it... Could it cause a custom class not to be handled as it should be?... No, that specifically should not cause ref_ptr to not delete your custom class when it needs to. It's just that users will be able to delete it even if it's still in the scene graph, which means probably on the next frame your program will crash... And another point : what if an update callback is still running when the object is dereferenced? Will it be dereferenced too?... You need to be careful not to invalidate iterators and not to delete an object from within its own callback. In general you will want to gather nodes in visitors / traversals, put them in a list and then remove them from the scene graph separately once the traversal has ended. Other than that, could you give us the full code of a small test program to see why it's leaking, or even if it's really leaking? What do you use to see if it's leaking? Some tools have trouble understanding ref_ptrs and will report leaks where there are none... Hope this helps, J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
Ku Krapox wrote: @ Paul : I've read your QSG, and found lots of useful information, thanks again! In particular I've learnt by heart the part on memory management. :P But the problem is, I'm doing everything right... All my objects are now in ref_ptr and I still have memory leaks. So, you're clearing a ref_ptr, and stepping through it in the debugger, and you're watching the reference count go to zero, but delete isn't being called? Please post a reproducer code that shows this behavior. I had an idea : might inheritance interfere with the osg::Referenced system? I mean, if I create my own class derived from PAT, then put it in a ref_ptr, will it be handled correctly? Any class derived from Referenced will work with OSG's memory management system. 'cause I've found out the following behaviour : class CustomPAT : public osg::PositionAttitudeTransform {...} int main() { PositionAttitudeTransform* myPAT1 = new PositionAttitudeTransform(); delete myPAT1; // Compilation error -> normal CustomPAT* myPAT2 = new CustomPAT(); delete myPAT2; // Compilation OK !!! -> shouldn't be } Looks like you failed to declare your destructor protected. -- -Paul Martz Skew Matrix Software http://www.skew-matrix.com/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
dglenn wrote: > > Memory handling has always been my worse subject in CS! > Not my best one either... :( @ Paul : I've read your QSG, and found lots of useful information, thanks again! In particular I've learnt by heart the part on memory management. :P But the problem is, I'm doing everything right... All my objects are now in ref_ptr and I still have memory leaks. I had an idea : might inheritance interfere with the osg::Referenced system? I mean, if I create my own class derived from PAT, then put it in a ref_ptr, will it be handled correctly? 'cause I've found out the following behaviour : class CustomPAT : public osg::PositionAttitudeTransform {...} int main() { PositionAttitudeTransform* myPAT1 = new PositionAttitudeTransform(); delete myPAT1; // Compilation error -> normal CustomPAT* myPAT2 = new CustomPAT(); delete myPAT2; // Compilation OK !!! -> shouldn't be } I don't know what to think about it... Could it cause a custom class not to be handled as it should be?... And another point : what if an update callback is still running when the object is dereferenced? Will it be dereferenced too?... -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=27977#27977 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
Wouzz wrote: > > I have a subsidiary question : let be a node defined inside a method, > existing only as a child of a group. Is this node deleted after calling the > removeChild method? 'cause I'm doing this everywhere... > > -- > void test() > { > ref_ptr myNode = new Node(); > ref_ptr myGroup = new Group(); > > myGroup->addChild(myNode); > > // ... > > myGroup->removeChild(myNode); > } > > int main() > { > > test(); > > // At this point, is myNode still in memory ??? > > } > - > Ku Yes, I've used that and it does clear the node - as far as I can tell. I used it when I had to clear an object in a tree. Then I used new to use that child again. I've also used removeChildren to clear all the children in a group. I've found this useful for clearing primitives in a hud for example. So far, I've checked this out using 'top' and it looks like it works for me! If I'm wrong, please someone please clarify this to me! Memory handling has always been my worse subject in CS! D Glenn D Glenn (a.k.a David Glenn) - Join the Navy and See the World ... from your Desk! -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=27954#27954 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
Hi again, thanks for your responses! @ Paul : A good piece of advice for sure... I didn't even know there was a quick start guide :-* Thank you. @ Skylark : Wow thanks a lot for this very clear explanation!! It seems I was doing exactly the wrong thing... plus, I had simple pointers everywhere. So I fixed this, all of my osg objects are now in osg::ref_ptr, and I've removed every useless operation on my pointers, no release, no delete, just calling "new" when needed. But I still have the memory leak! (which is proven to be caused by my object) Any idea?... I have a subsidiary question : let be a node defined dynamically, existing only as a child of a group. Does the removeChild method delete this node? 'cause I'm doing this everywhere... Ku -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=27948#27948 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
Ku Krapox wrote: Hi all, It's me again, with another practical problem : how to delete properly my osg objects when they contain lots of stuff? Basically I have a PAT with lots of groups and nodes inside it. At some point I need to delete it, then reinstanciate it right away. But I found out that doing this produces one big memory leak... My object is in an osg::ref_ptr, so I tried to .release() it first, it didn't quite change anything... I also tried to mess with the destructor, deleting manually some of the groups and nodes, with no more success... I have to mention that I use several NodeCallbacks inside my PAT, which may be active when I delete it. I don't know how to deal with them, or if I have to... So my question is : what is the proper method to delete an osg scene or part of scene (in order to avoid memory leaks)? Do I have to access and delete every leaf of my graph? Thanks for any advice! :) I'd advise you to read the OSG Quick Start Guide. http://stores.lulu.com/store.php?fGroupID=2076 -- -Paul Martz Skew Matrix Software http://www.skew-matrix.com/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
Hello Ku, In addition to Torben's answer, which was good, I'll add this: My object is in an osg::ref_ptr, so I tried to .release() it first, it didn't quite change anything... Actually it did: that's what introduced your large memory leak. ref_ptr::release() tells that ref_ptr to stop managing the ref count of the object it points to (i.e. don't delete it when its ref count falls to 0). This is only useful when you'll be assigning the pointer to another ref_ptr in the near future, otherwise as you've seen it leaks the whole graph which has that pointer as its root... ref_ptr::release() should really only be used when returning a ref_ptr from a function as a raw pointer, and when you know you'll assign the return value of the function to another ref_ptr. For example: osg::Node* createSomeSubgraph() { osg::ref_ptr root = new osg::Group; // ... create the subgraph under root return root.release(); } int main(...) { // ... osg::ref_ptr subgraph = createSomeSubgraph(); // ... } Now that I've explained the results you got, back to what you want. As Torben said, just assign some other pointer to your existing ref_ptr. For example: osg::ref_ptr root = new osg::Group; // ... Use root for some time root = new osg::Group; At that point, if the pointer in root has a ref count of 1 (i.e. the ref_ptr called "root" is the only one that has that pointer), at the assignment of another pointer, the ref count will fall to 0 and it will be deleted. If its ref count is not 1, then it won't be deleted after the assignment, and you'll be really happy you used ref_ptrs because other entities using that pointer will happily continue using it until they're done, instead of using a dangling pointer to some memory you would have freed... But you can keep using root without worrying about it, knowing that the subgraph has either been deleted or will be deleted in the near future. Hope this helps, J-S -- __ Jean-Sebastien Guayjean-sebastien.g...@cm-labs.com http://www.cm-labs.com/ http://whitestar02.webhop.org/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] How to delete osg graphs properly?
Hi, hold your osg nodes (or the root node of your complete scenegraph) with osg reference pointer (osg::ref) instead standard pointers. Assign NULL to this pointer, than the ref pointer doesn't point any longer the node, and the node will be destroyed - unless you point with other pointers to this node :) Cheers, Torben -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=27927#27927 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
[osg-users] How to delete osg graphs properly?
Hi all, It's me again, with another practical problem : how to delete properly my osg objects when they contain lots of stuff? Basically I have a PAT with lots of groups and nodes inside it. At some point I need to delete it, then reinstanciate it right away. But I found out that doing this produces one big memory leak... My object is in an osg::ref_ptr, so I tried to .release() it first, it didn't quite change anything... I also tried to mess with the destructor, deleting manually some of the groups and nodes, with no more success... I have to mention that I use several NodeCallbacks inside my PAT, which may be active when I delete it. I don't know how to deal with them, or if I have to... So my question is : what is the proper method to delete an osg scene or part of scene (in order to avoid memory leaks)? Do I have to access and delete every leaf of my graph? Thanks for any advice! :) Cheers, Ku -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=27926#27926 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org