Stepping through the debugger, the BBox object is okay at the entry of the function, but as soon as it enters the function, the vfptr object points to 0xccccc. I don’t get it.
- What is causing this ?
- Why is there a virtual table reference in there when the object is not derived from other class. (Though, it resides in GameObject from which my Player class inherits and I retrieve the BBox from within player. But, why does the BBox have the reference ? Shouldn’t it be player who should be maintained in that reference ?)
For 1; some code for reference:
A. I retrieve the bounding box from player. This returns a bounding box as expected. I then send its address to GetGridCells.
const BoundingBox& l_Bbox = l_pPlayer->GetBoundingBox();
boost::unordered_set < Cell*, CellPHash >& l_GridCells = GetGridCells ( &l_Bbox );
B. This is where a_pBoundingBox goes crazy and gets that garbage value.
boost::unordered_set< Cell*, CellPHash > CollisionMgr::GetGridCells(const BoundingBox *a_pBoundingBox)
{
I think the following code is also pertinent, so I’m sticking this in here anyways:
const BoundingBox& Player::GetBoundingBox(void)
{
return BoundingBox( &GetBoundingSphere() );
}
const BoundingSphere& Player::GetBoundingSphere(void)
{
BoundingSphere& l_BSphere = m_pGeomMesh->m_BoundingSphere;
l_BSphere.m_Center = GetPosition();
return l_BSphere;
}
// BoundingBox Constructor
BoundingBox(const BoundingSphere* a_pBoundingSphere);
Can anyone please give me some idea as to why this is happening? Also, if you want me to post more code, please do let me know.
Thanks!
Here, you’re returning a reference to a temporary
BoundingBoxobject. That object goes out of scope as soon as thereturnstatement ends.Return a
BoundingBoxinstead of aBoundingBox&instead.Also:
Here, you take a reference to the bounding sphere of the
m_pGeomMesh, then modify the value it refers to. This will result in a modification of the original object. Are you sure this is what you want?Also:
In the only place where using a reference makes a great deal of sense, you use a pointer instead. Why?