Hello I am trying to implement object-picking in C++ as described in the manual. Unfortunately even if I follow the code exactly some mysterious access violation would occur. I discovered that the problem is with CollisionNode: simply creating a CollisionNode object on the stack would cause corrupted stack and buffer overrun issues, while creating it on the heap would cause access violation in some random location.
I am using VS 2008 having no issue so far with Panda3D except this. I tried both Panda 1.7.2 and 1.8.0 and the issue persists. Thank you for any help in advance.
Hi, welcome to the forums!
CollisionNode is a reference counted class. You shouldn’t attempt to create it on the stack. When creating it, make sure that you hold a reference to it by using a special PointerTo<> pointer, like:
PT(CollisionNode) cnode = new CollisionNode("name");
Then treat cnode as if it were an ordinary pointer. It will automatically be destructed when all PT() objects pointing to it have destructed, so never use the “delete” keyword on a reference counted class! It will cause a double free, which could also be what is causing your crash.
If you don’t use PT() pointers for classes that inherit from ReferenceCount, then it will think that you don’t hold any references to it and it will destruct automatically. That could also cause a crash like that.
Thank you for your reply and welcome.
Actually I did create CollisionNode with PT(CollisionNode) as you suggest (as in the manual example) at first but it caused some access violation crash. I was working on some other code and after seeing your post I tried the picking code again. Amazingly there is no more crash even though I am sure it is the same as before. I even tried creating CollisionNode on the stack again and as you point out it would cause some stack corruption but even more amazingly I can continue to run the program which I couldn’t before.
So my conclusion is either it is the other code I worked on or your spooky-action-at-a-distance magical power. At this point I tend to believe it is the latter.
Anyway as you can see I am obviously new to panda3D I worked as a game developer for a few years in Hong Kong unfortunately the company “crashed” and it was difficult to find a decent game developing position here and didn’t work as a programmer for 2 years just getting my hands on programming again with panda3D and other stuff and I am going to study at UC San Diego for a MS in CS this fall.
The crash resurfaced again and finally I figured out where the problem was. When I first downloaded and built the " hello world" Panda3D program I couldn’t build it because of the HAVE_PYTHON definition and I didn’t include the relevant python libraries. Instinctively I took out HAVE_PYTHON since I am only using C++ and everything worked fine until I started using PandaNode, which may or may not cause runtime crashes.