我在这里遇到了一个对我来说很奇怪的问题,我故意更改了我的程序以使其使用动态内存,因为我之前遇到过堆栈错误。内存密集型部分已更改为动态,但我仍然遇到堆栈溢出,而我预计堆溢出,但我有足够的 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;
}
}
}
如果我删除(或大幅减少)双数组,那么错误就消失了。
编辑:我知道我还没有清理内存,首先想让应用程序实际运行。