Re: [osg-users] dynamic_cast of referenced objects
Hi Keven, It sounds it you were modifying the text on each frame without setting the DataVariance to DYNAMIC, and you were running the viewer multithreaded so the draw dispatch ended overlapping with the update of the text. Robert. On Tue, Feb 9, 2010 at 11:57 PM, Kevin Wilder kevin_wil...@hotmail.com wrote: Hi, For anyone who is still following this thread (or for anyone who stumbles upon it while trying to solve a similar problem in the future), I figured I'd post my findings: The bit about the object reference pointers vs. standard C++ pointers turned out to be a red herring. Seems the crashes resulted from my method of updating 2D text in the scene. I was just calling -settext(blah) from the main loop whenever I wanted to change the displayed characters. The crashes went away when I implemented a callback function to update the text. Here's an example of my text update callback function: Code: struct TextUpdateCallback : public osg::Drawable::UpdateCallback { virtual void update(osg::NodeVisitor* nv, osg::Drawable* drawable) { osgText::Text * TextPointer = dynamic_castosgText::Text*(drawable) ; if ( TextPointer ) { TextPointer-setText(NewText) ; TextPointer-setColor(NewColour) ; } } } I hope this helps. Cheers, Kevin[/quote] -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=23879#23879 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] dynamic_cast of referenced objects
Hi, For anyone who is still following this thread (or for anyone who stumbles upon it while trying to solve a similar problem in the future), I figured I'd post my findings: The bit about the object reference pointers vs. standard C++ pointers turned out to be a red herring. Seems the crashes resulted from my method of updating 2D text in the scene. I was just calling -settext(blah) from the main loop whenever I wanted to change the displayed characters. The crashes went away when I implemented a callback function to update the text. Here's an example of my text update callback function: Code: struct TextUpdateCallback : public osg::Drawable::UpdateCallback { virtual void update(osg::NodeVisitor* nv, osg::Drawable* drawable) { osgText::Text * TextPointer = dynamic_castosgText::Text*(drawable) ; if ( TextPointer ) { TextPointer-setText(NewText) ; TextPointer-setColor(NewColour) ; } } } I hope this helps. Cheers, Kevin[/quote] -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=23879#23879 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] dynamic_cast of referenced objects
Kevin, Thanks for sharing. I'm sure it will come in handy...:) Regards, -Shayne -Original Message- From: osg-users-boun...@lists.openscenegraph.org [mailto:osg-users-boun...@lists.openscenegraph.org] On Behalf Of Kevin Wilder Sent: Tuesday, February 09, 2010 4:57 PM To: osg-users@lists.openscenegraph.org Subject: Re: [osg-users] dynamic_cast of referenced objects Hi, For anyone who is still following this thread (or for anyone who stumbles upon it while trying to solve a similar problem in the future), I figured I'd post my findings: The bit about the object reference pointers vs. standard C++ pointers turned out to be a red herring. Seems the crashes resulted from my method of updating 2D text in the scene. I was just calling -settext(blah) from the main loop whenever I wanted to change the displayed characters. The crashes went away when I implemented a callback function to update the text. Here's an example of my text update callback function: Code: struct TextUpdateCallback : public osg::Drawable::UpdateCallback { virtual void update(osg::NodeVisitor* nv, osg::Drawable* drawable) { osgText::Text * TextPointer = dynamic_castosgText::Text*(drawable) ; if ( TextPointer ) { TextPointer-setText(NewText) ; TextPointer-setColor(NewColour) ; } } } I hope this helps. Cheers, Kevin[/quote] -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=23879#23879 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org smime.p7s Description: S/MIME cryptographic signature ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] dynamic_cast of referenced objects
Hi, Thank you both for all of your help! Cheers, Kevin -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=23805#23805 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] dynamic_cast of referenced objects
HomerSimpson wrote: My project is throwing an occasional unhandled win32 exception error and I've been trying to figure out why. One thing you should check is if you have any raw C++ pointers pointing to objects that are also pointed to by ref_ptr's. Accessing the object through the raw pointer after that the ref_ptr's have gone out of scope is illegal since the object is automaticly destroyed when the last ref_ptr goes out of scope. Notice that since OSG uses ref_ptr's in most places any object that you add to a scenegraph will most likely be referenced by a ref_ptr. You should therefore try to avoid using raw pointers to such objects. It's safer to use ref_ptr's. Example: the following will fail Code: osg::Node* myTransform = new osg::MatrixTransform(); { osg::ref_ptr osg::Group myGroup = new osg::Group(); myGroup-addChild(myTransform); } myTransform-someMethod(); // Wrong. Object has been deleted. In the example above replace the first line with: Code: osg::ref_ptr osg::Node myTransform = new osg::MatrixTransform(); and use myTransform.get() inside the addChild call. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=23809#23809 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] dynamic_cast of referenced objects
On Sat, Feb 6, 2010 at 4:07 AM, Kevin Wilder kevin_wil...@hotmail.comwrote: Hi, ... I came upon a passage in the OpenSceneGraph Quick Start Guide that says: Be careful when returning the address of an object from a function. If you do this incorrectly, the ref_ptr storing the memory address could go out of scope before the address is placed on the return stack. This is talking about the following anti-pattern: osg::Node* func() { osg::ref_ptrNode result; ... return result.get(); // result gets deleted! } Instead of returning result.get() you need to return result.release() which prevents the ref_ptr destructor from deleting the object. As a side node, I think this idiom is unfortunate; ref_ptrs should be returned by loading and scene graph creation functions that run in the database pager thread. Live objects with a ref count of 0 are a bad thing. I assume that this also applies when passing referenced objects to a function... But most of the examples of callback functions that I have seen on the web all use standard C++ pointers in the function definition, rather than ref_ptr. For example: --- snip --- void operator()( osg::Node* node, osg::NodeVisitor * nv ) { osg::MatrixTransform * MatrixXform = dynamic_castosg::MatrixTransform*(node) ; if ( MatrixXform ) This style of code is common in OSG. It is safe because the caller has a ref_ptr somewhere that points to the node. --- snip --- Thinking that this may be the source of my dangling pointer, I tried to modify my own callback function definition to use referenced object pointers per the quick start guide's recommendations. Unfortunately, I got hung up on the dynamic_cast in the code above. I can't figure out how to cast the osg::node referenced object pointer into an osg::MatrixTransform referenced object pointer. Is there an equivalant means of casting an osg::ref_ptrosg::node into an osg::ref_ptrosg::MatrixTransform when using referenced object pointers? I'm trying to do something like: --- snip --- void operator()( osg::ref_ptrosg::Node node, osg::ref_ptrosg::NodeVisitor nv ) { osg::ref_ptrosg::MatrixTransform MatrixXform = dynamic_castosg::ref_ptrosg::MatrixTransform(node) ; if ( MatrixXform ) --- snip --- If you still want to do that, you can say osg::ref_ptrosg::MatrixTransform MatrixXform = dynamic_castosg::MatrixTransform*(node.get()); Thanks for the help. I'm relatively new to OpenSceneGraph programming. Cheers, Kevin Tim ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] dynamic_cast of referenced objects
Hi Kevin, My project is throwing an occasional unhandled win32 exception error and I've been trying to figure out why. There is no consistency in the timing so I assume that I have a dangling pointer somewhere. I doubt you're on the right track with changing standard pointers to ref_ptrs in function arguments, but when unsure it's better to use ref_ptrs everywhere. The right way to debug your error would be to compile a debug version and run your code in a debugger. Then the debugger would likely tell you exactly which pointer was left dangling, you'd have a stack trace, and it would be relatively easy to track down the cause. Just sprinkling ref_ptrs everywhere in the hope that it'll fix it is not a solution, IMHO. But to answer your more concrete question: Is there an equivalant means of casting an osg::ref_ptrosg::node into an osg::ref_ptrosg::MatrixTransform when using referenced object pointers? I'm trying to do something like: --- snip --- void operator()( osg::ref_ptrosg::Node node, osg::ref_ptrosg::NodeVisitor nv ) { osg::ref_ptrosg::MatrixTransform MatrixXform = dynamic_castosg::ref_ptrosg::MatrixTransform(node) ; if ( MatrixXform ) --- snip --- osg::ref_ptrosg::MatrixTransform MatrixXform = dynamic_castosg::MatrixTransform*(node.get()); A ref_ptr is an object on the stack (not a pointer) that tracks a pointer. And ref_ptr::get() returns a raw pointer to the underlying object. So you can construct (or here, assign to) a ref_ptr from a raw pointer. So from right to left: 1. You extract the raw Node* pointer from a ref_ptrNode 2. You dynamic_cast that to a MatrixTransform* 3. You assign that to a ref_ptrMatrixTransform 4. Bob is your uncle. Note that in the process, the ref count of node will be incremented if it was really a MatrixTransform (i.e. if the assignment assigned something other than NULL), so your function will have 2 refs (one in the temporary ref_ptrNode, and one in the ref_ptrMatrixTransform). This is unnecessary, as the temporary ref_ptrNode in the function arguments guarantees that the object will not be deleted in the scope of the function. So you could do this instead: osg::MatrixTransform* MatrixXform = dynamic_castosg::MatrixTransform*(node.get()); As I said, storing it in a raw pointer is safe at that point because of the temporary ref_ptrNode in the function arguments which will be destroyed when the function ends. So you see, ref_ptrs require a bit of work wrapping your mind around them, but it's not magic and it all works really well. 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