Render2D - Access violation

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);

}

I don’t see anything obviously wrong here (though you should probably save the lens object in a PT(OrthographicLens) and your display region object in a PT(DisplayRegion) to hold their reference counts safely).

Can you determine the call stack at the time of the crash? That might provide some insight.

Are you sure you are compiling in MSVS 2008 with a Release build with NDEBUG removed from the project settings? Failing to do all three of these is a common mistake. Particularly, failing to remove NDEBUG is a common mistake, and it can lead to mysterious crashes like this.

David

I am certain I am compiling in VS2008 release.
I didn’t see any NDEBUG definition, and couldn’t find any clear steps to removing it.

Perhaps that is my problem afterall.

Any tips on how to remove this mysterious definition?

More research revealed how to remove this definition (preprocessor section of the build properties.

And, yes, this did solve my problem.

I don’t if I should weep or cheer - wasted days trying to solve this thing… I should have posted earlier.

Thanks very much!