gpt4 book ai didi

c++ - 预期(至多)堆溢出时堆栈溢出

转载 作者:行者123 更新时间:2023-11-30 01:20:23 28 4
gpt4 key购买 nike

我在这里遇到了一个对我来说很奇怪的问题,我故意更改了我的程序以使其使用动态内存,因为我之前遇到过堆栈错误。内存密集型部分已更改为动态,但我仍然遇到堆栈溢出,虽然我预计会发生堆溢出,但我有足够的 RAM,所以即使是那些也不应该发生。

头文件:

#ifndef __FPS_h_
#define __FPS_h_

#include "BaseApplication.h"

#include <Ogre/OgreLogManager.h>
#include <Ogre/OgreInstanceManager.h>

#include <btBulletDynamicsCommon.h>
#include <btIDebugDraw.h>

#include "BtOgreExtras.h"
#include "BtOgreGP.h"
#include "btOgrePG.h"

class FPS : public BaseApplication
{
public:
FPS(void);
virtual ~FPS(void);

protected:
virtual void createScene(void);
virtual void createCamera(void);
virtual void createViewports(void);

virtual bool frameRenderingQueued(const Ogre::FrameEvent &evt);

void initializePhysics(void);
void initializeGraphics(void);
void initializeGraphicsBase(void);
void initializeGraphicsGround(void);
void initializeGraphicsTiles(void);
void initializeGraphicsRobot(void);

private:
Ogre::InstanceManager* tileInstanceManager;
Ogre::SceneNode*** tileSceneNode[300][300];
btRigidBody*** tileRigidBody[300][300];
//Ogre::SceneNode* tileSceneNode;
//btRigidBody* tileRigidBody;

Ogre::SceneNode* robotSceneNode;
btRigidBody* robotRigidBody;

btDiscreteDynamicsWorld* dynamicsWorld;
BtOgre::DebugDrawer* debugDrawer;
};

#endif // #ifndef __FPS_h_

部分源文件:

void FPS::initializeGraphicsTiles() {
//tile instance manager
uint16_t flags = Ogre::InstanceManagerFlags::IM_USEALL;
tileInstanceManager = mSceneMgr->createInstanceManager("InstanceManager", "tile.mesh", Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, Ogre::InstanceManager::HWInstancingVTF, 400, flags);

//fake tile
Ogre::Entity* entFakeTile = mSceneMgr->createEntity("Tile", "tile.mesh");

//initialize arrays
**tileSceneNode = new Ogre::SceneNode**[300];
**tileRigidBody = new btRigidBody**[300];

//tile
for (int x = 0; x <= 300; x++) {
*tileSceneNode[x] = new Ogre::SceneNode**[300];
*tileRigidBody[x] = new btRigidBody**[300];
for (int z = 0; z <= 300; z++) {
Ogre::InstancedEntity* entTile = tileInstanceManager->createInstancedEntity("Examples/Instancing/VTF/HW/Robot");
entTile->setCastShadows(true);

Ogre::SceneNode* tempNode = mSceneMgr->getRootSceneNode()->createChildSceneNode();
tempNode->attachObject(entTile);
**tileSceneNode[x][z] = tempNode;

BtOgre::StaticMeshToShapeConverter converter(entFakeTile);
btConvexHullShape* mShape = converter.createConvex();
btScalar mass = 100;
btVector3 inertia;
BtOgre::RigidBodyState* state = new BtOgre::RigidBodyState(tempNode);
btRigidBody* tempBody = new btRigidBody(mass, state, mShape, inertia);

btTransform originalTransform;
tempBody->getMotionState()->getWorldTransform(originalTransform);
btVector3 originalOrigin = originalTransform.getOrigin();
originalOrigin.setX(originalOrigin.getX() + (x * 10.0));
originalOrigin.setZ(originalOrigin.getZ() + (z * 10.0));
originalTransform.setOrigin(originalOrigin);
tempBody->getMotionState()->setWorldTransform(originalTransform);

dynamicsWorld->addRigidBody(tempBody);
**tileRigidBody[x][z] = tempBody;
}
}
}

如果我删除(或大幅减少) double 组,那么错误就消失了。

编辑:我知道我还没有清理内存,首先想让应用程序实际运行。

最佳答案

查看 sizeof(FPS) .我猜它相当大(> 1MB)并且您正在分配 FPS 的实例自己在堆栈上,对吧?你在哪里使用FPS app; , 将其替换为 std::unique_ptr<FPS> app_ptr( new FPS ); ,那应该可以解决您的问题。

关于c++ - 预期(至多)堆溢出时堆栈溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19502641/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com