renderer access

I’m really confused with the output of this code

#include "pandaFramework.h"
#include "pandaSystem.h"

PandaFramework g_framework; 

int main(int argc, char *argv[])
{
  // setup the basic window display
  g_framework.open_framework(argc, argv);
  g_framework.set_window_title("My Panda3D Window");  
  WindowFramework *window = g_framework.open_window();

  // strange stuff incoming
  cout << "debug1 " << &(window->get_render()) << endl;
  cout << "debug2 " << &(window->get_render()) << endl;
  
  getchar();

  return (0);
}

which is

debug1 0012FF40
debug2 0012FF4C

Now how can this happen ? No matter how often I access the renderer’s address, the last call will always yield a different value then all the others before it (which all have the same value). Flushing the std out stream did not make any difference.

Look at the definition of get_render in windowFramework.h:

public:
  NodePath get_render();

This means it returns a new NodePath on the stack, that points to the PandaNode “render”. So, when you’re doing &(window->get_render()), you’re referencing that brand new NodePath.
This means that every time you’re doing that, you are printing a reference to a new NodePath.

Now if the definition would look like this (but it doesn’t!)

public:
  NodePath &get_render();

That would mean that the function would return a reference to a NodePath instead copying the result to a brand new NodePath. It if were like this, your code would print out the same every time.

But in any case, get_render().node() would actually have printed out the same every time, as the actual PandaNode “render” is still the same.

That makes sense but would this not mean that successive calls to

&(window->get_render())

would yield different results every time, no matter how often I call it ?

Maybe the framework somehow detects reusage and tries to save up memory thus reusing the “old” object but then 8 successive calls should not produce 2 different results - first 7 calls the same value, only last call different one.

It’s not the Panda framework that defines where in memory the object goes, that’s your operating system’s memory allocation functions.

This is absolutely normal C++ behavior. It seems you are not used to functions that return concrete objects.

With a concrete object return–that is, neither a pointer nor a reference–the object you get back is a temporary object allocated on the stack. So its address is largely meaningless, but if you were to query its address anyway, you would receive a pointer into the stack (or into some other temporary memory space set aside by your compiler). Since this is a temporary memory space, the addresses here get reused from time to time, so you might see the different address or the same address come back from this call, at the compiler’s whim.

David

Right, I did not pay attention to what I was getting back from get_render() as I expected a reference to the actual object. I just understood how this whole nodepath<->node system works so I did not pay enough attention to it previously. Thanks for the help guys :slight_smile: