Sign in to follow this  
guitarguy

please help with some OO primitive shapes

Recommended Posts

hello, im still trying to get a working class of a shape i made, using vertices in directx/c++. i still dont get why there are no tutorials on putting your shapes into a class, so you can dish out as many instances of that shape you want into different locations, sizes, colors etc. and im beginning to think that there is some easier method of doing this that i dont know about, because i really cant find a trace of this stuff online. so the problem im having at the minute, is that ive tried to make this class, of a cylinder using one of the directx sdk examples and building up on it. im getting an unhandled exception. the exact error is: First-chance exception at 0x00411c2d in Block Games.exe: 0xC0000005: Access violation reading location 0x00000000. Unhandled exception at 0x00411c2d in Block Games.exe: 0xC0000005: Access violation reading location 0x00000000. visual studio then points me to line 143. i dont know why though, because i think its something to do with a malformed vertex buffer when i make the second object (bg2). if i highlight bg2 on line 254, a box appears that shows the following text: vertex_buffer = CXX0030: Error: expression cannot be evaluated and finally, if i comment out the line of code on line 342, the program runs, and one cylinder is displayed (though if two were displayed, would they be in the same place so that i wouldnt be able to see both?). here is the pastebin of the code: http://gamedev.pastebin.com/m7057cd9c if anybody knows whats wrong, please tell me cheers

Share this post


Link to post
Share on other sites
Runtime errors reporting access violations around the location 0x00000000 is a near sure sign that you're trying to dereference a null pointer.

In this case that pointer is bg2. It is null because it is never set to point to anything else.

See here:

//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Creates the scene geometry
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
bg = new block_geometry;
if(bg == 0)
return E_FAIL;
else
return S_OK;

// BELOW THIS IS ALL DEAD CODE.
bg2 = new block_geometry;
if(bg2 == 0)
return E_FAIL;
else
return S_OK;


return S_OK;
}

So bg2 will never be assigned to point to the new object because either:

A) The top-most new allocation will fail and the std::bad_alloc exception will be thrown, or
B) The top-most conditional will cause the function to return with S_OK.

Note: There is no point testing whether the pointer is null in this function, if new fails then the exception is thrown so the if statement is never actually reached, or if it is reached, then you know that the allocation succeeded so there's no pointing in testing for it.

Share this post


Link to post
Share on other sites
HRESULT InitGeometry()
{
bg = new block_geometry;
if(bg == 0)
return E_FAIL;
else
return S_OK;
// the returns above are the deadends of the road. function quits, simple.

bg2 = new block_geometry;
if(bg2 == 0)
return E_FAIL;
else
return S_OK;


return S_OK;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this