使用“new”创建实例后出现堆栈溢出错误
- 编辑 - 代码在这里看起来很奇怪,所以我建议直接在给定的链接中查看文件。
在开发引擎时,我遇到了一个无法解决的问题。希望在不进行任何大量修改的情况下解决此问题,代码如下。
void Block::DoCollision(GameObject* obj){
obj->DoCollision(this);
}
这就是堆栈溢出发生的地方。这个应用程序工作得很好,直到我使用 new 关键字创建该类的两个实例。如果我只有该类的 1 个实例,它就可以正常工作。
Block* a = new Block(0, 0, 0, 5);
AddGameObject(a);
a = new Block(30, 0, 0, 5);
AddGameObject(a);
这些参数只是 x、y、z 和大小。
代码是事先检查过的。只有具有匹配 Collisonflag 和碰撞类型的对象才会触发 DoCollision();功能。
((*list1)->m_collisionFlag & (*list2)->m_type)
也许我的支票搞砸了。我在这里附加了相关文件 http://celestialcoding.com/index.html php?topic=1465.msg9913;topicseen#new。您无需注册即可下载它们。主要嫌疑人,我也粘贴了下面的代码。
来自 GameManager.cpp
void GameManager::Update(float dt){
GameList::iterator list1;
for(list1=m_gameObjectList.begin(); list1 != m_gameObjectList.end(); ++list1){
GameObject* temp = *list1;
// Update logic and positions
if((*list1)->m_active){
(*list1)->Update(dt);
// Clip((*list1)->m_position); // Modify for bounce affect
} else continue;
// Check for collisions
if((*list1)->m_collisionFlag != GameObject::TYPE_NONE){
GameList::iterator list2;
for(list2=m_gameObjectList.begin(); list2 != m_gameObjectList.end(); ++list2){
if(!(*list2)->m_active)
continue;
if(list1 == list2)
continue;
if( (*list2)->m_active &&
((*list1)->m_collisionFlag & (*list2)->m_type) &&
(*list1)->IsColliding(*list2)){
(*list1)->DoCollision((*list2));
}
}
}
if(list1==m_gameObjectList.end()) break;
}
GameList::iterator end = m_gameObjectList.end();
GameList::iterator newEnd = remove_if(m_gameObjectList.begin(),m_gameObjectList.end(),RemoveNotActive);
if(newEnd != end)
m_gameObjectList.erase(newEnd,end);
}
void GameManager::LoadAllFiles(){
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Top.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Right.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Back.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Left.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Front.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Bottom.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Textures/Terrain1.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Textures/Terrain2.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Details/TerrainDetails.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Textures/Water1.bmp", GetNextFreeID());
Block* a = new Block(0, 0, 0, 5);
AddGameObject(a);
a = new Block(30, 0, 0, 5);
AddGameObject(a);
Player* d = new Player(0, 100,0);
AddGameObject(d);
}
void Block::Draw(){
glPushMatrix();
glTranslatef(m_position.x(), m_position.y(), m_position.z());
glRotatef(m_facingAngle, 0, 1, 0);
glScalef(m_size, m_size, m_size);
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(m_boundingRect.left, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.left, m_boundingRect.bottom, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.bottom, m_position.z());
glVertex3f(m_boundingRect.left, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.left, m_boundingRect.bottom, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.bottom, m_position.z());
glEnd();
// DrawBox(m_position.x(), m_position.y(), m_position.z(), m_size, m_size, m_size, 8);
glPopMatrix();
}
void Block::DoCollision(GameObject* obj){
GameObject* t = this; // I modified this to see for sure that it was causing the mistake.
// obj->DoCollision(NULL); // Just revert it back to
/*
void Block::DoCollision(GameObject* obj){
obj->DoCollision(this);
}
*/
}
- EDIT - The code looks strange here, so I suggest viewing the files directly in the link given.
While working on my engine, I came across a issue that I'm unable to resolve. Hoping to fix this without any heavy modification, the code is below.
void Block::DoCollision(GameObject* obj){
obj->DoCollision(this);
}
That is where the stack overflow occurs. This application works perfectly fine until I create two instances of the class using the new keyword. If I only had 1 instance of the class, it worked fine.
Block* a = new Block(0, 0, 0, 5);
AddGameObject(a);
a = new Block(30, 0, 0, 5);
AddGameObject(a);
Those parameters are just x,y,z and size.
The code is checked before hand. Only a object with a matching Collisonflag and collision type will trigger the DoCollision(); function.
((*list1)->m_collisionFlag & (*list2)->m_type)
Maybe my check is messed up though. I attached the files concerned here http://celestialcoding.com/index.php?topic=1465.msg9913;topicseen#new. You can download them without having to sign up. The main suspects, I also pasted the code for below.
From GameManager.cpp
void GameManager::Update(float dt){
GameList::iterator list1;
for(list1=m_gameObjectList.begin(); list1 != m_gameObjectList.end(); ++list1){
GameObject* temp = *list1;
// Update logic and positions
if((*list1)->m_active){
(*list1)->Update(dt);
// Clip((*list1)->m_position); // Modify for bounce affect
} else continue;
// Check for collisions
if((*list1)->m_collisionFlag != GameObject::TYPE_NONE){
GameList::iterator list2;
for(list2=m_gameObjectList.begin(); list2 != m_gameObjectList.end(); ++list2){
if(!(*list2)->m_active)
continue;
if(list1 == list2)
continue;
if( (*list2)->m_active &&
((*list1)->m_collisionFlag & (*list2)->m_type) &&
(*list1)->IsColliding(*list2)){
(*list1)->DoCollision((*list2));
}
}
}
if(list1==m_gameObjectList.end()) break;
}
GameList::iterator end = m_gameObjectList.end();
GameList::iterator newEnd = remove_if(m_gameObjectList.begin(),m_gameObjectList.end(),RemoveNotActive);
if(newEnd != end)
m_gameObjectList.erase(newEnd,end);
}
void GameManager::LoadAllFiles(){
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Top.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Right.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Back.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Left.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Front.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Models/Skybox/Images/Bottom.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Textures/Terrain1.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Textures/Terrain2.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Details/TerrainDetails.bmp", GetNextFreeID());
LoadSkin(m_gameTextureList, "Terrain/Textures/Water1.bmp", GetNextFreeID());
Block* a = new Block(0, 0, 0, 5);
AddGameObject(a);
a = new Block(30, 0, 0, 5);
AddGameObject(a);
Player* d = new Player(0, 100,0);
AddGameObject(d);
}
void Block::Draw(){
glPushMatrix();
glTranslatef(m_position.x(), m_position.y(), m_position.z());
glRotatef(m_facingAngle, 0, 1, 0);
glScalef(m_size, m_size, m_size);
glBegin(GL_LINES);
glColor3f(255, 255, 255);
glVertex3f(m_boundingRect.left, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.left, m_boundingRect.bottom, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.bottom, m_position.z());
glVertex3f(m_boundingRect.left, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.left, m_boundingRect.bottom, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.top, m_position.z());
glVertex3f(m_boundingRect.right, m_boundingRect.bottom, m_position.z());
glEnd();
// DrawBox(m_position.x(), m_position.y(), m_position.z(), m_size, m_size, m_size, 8);
glPopMatrix();
}
void Block::DoCollision(GameObject* obj){
GameObject* t = this; // I modified this to see for sure that it was causing the mistake.
// obj->DoCollision(NULL); // Just revert it back to
/*
void Block::DoCollision(GameObject* obj){
obj->DoCollision(this);
}
*/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
堆栈溢出通常来自无限递归。我无法解析你的问题,但这是一个猜测:
Stack overflow usually comes from infinite recursion. I'm having trouble parsing your question, but here's a guess: