Problem:
When executing, I sometimes get this error immediately upon starting the framework.main_loop();
Unhandled exception at 0x01fe1fe9 in HelloWorld.exe: 0xC0000005: Access violation reading location 0x4dd2f1c6.
Note: The code runs fine maybe 50% of the time. But, restarting the application a few times always ends in this access violation.
The goal of the code was to simply add a few items as a HUD in the 2d display region. I first create my own display region with a new OrthographicLens, then add some text or button to it.
NOTE: if I do NOT add any text or buttons to this region, then the error never occurs.
Here is the code:
void HUDCreator::CreateHUD(WindowFramework * win) {
// method comes from a GraphicsOutput which is usually from a GraphicsWindow
// GrahpicsWindows is usually made from Graphicsengine::Make_window() function
GraphicsWindow * gfxWin = win->get_graphics_window();
DisplayRegion * dr = gfxWin->make_display_region();
dr->set_sort(20); // this is the Z-index of this region compared to other regions
OrthographicLens * lens = new OrthographicLens();
lens->set_film_size(2, 2);
lens->set_near_far(-1000, 1000); // the Y distance in which we can draw 2d stuff for layering
PT(Camera) cam = new Camera("myCam2D", lens);
NodePath myCamera2d = NodePath(cam);
NodePath myRender2d = NodePath("myRender2d");
myRender2d.set_depth_test(false);
myRender2d.set_depth_write(false);
myCamera2d.reparent_to(myRender2d);
dr->set_camera(myCamera2d);
// render2d - Y coordinate is always zero since there is no depth
// lower-left corner is -1,0,-1 and upper right is 1,0,1
AddSomethingToHUD(myRender2d);
//AddSomethingToHUD(win->get_aspect_2d());
}
void HUDCreator::AddSomethingToHUD(NodePath myRender2d) {
// TODO: add a text block in the middle of screen.
/*PT(TextNode) text;
text = new TextNode("Target Reticle");
text->set_text("[ ]");
NodePath textNodePath = myRender2d.attach_new_node(text);
textNodePath.set_scale(0.07);*/
// Creates a button
PT(PGButton) MyButton;
MyButton = new PGButton("My Button Name");
MyButton->setup("Button Text",.1);
NodePath defbutNP = myRender2d.attach_new_node(MyButton);
defbutNP.set_scale(0.1);
defbutNP.set_pos(-.9,0,-.9);
}