c语言字符串构造函数,c – std :: string构造函数会破坏指针

我有一个Entity类,它包含3个指针:m_rigidBody,m_entity和m_parent.在Entity :: setModel(std :: string model)的某个地方,它正在崩溃.显然,这是由m_entity中的错误数据引起的.奇怪的是,我在构造函数中将其弄糊涂了,从那时起就没有碰过它.我调试了它并在其上放置了一个观察点,并且发现m_entity成员正在std :: string的构造函数中被更改,这是在将const char *转换为用于setModel调用的std :: string时被调用的.我在Mac上运行,如果这有帮助(我想我记得Mac上的std :: string有些问题).关于发生了什么的任何想法?

编辑:这是GEntity的代码:

GEntity::GEntity(GWorld* world, unsigned long int idNum) {

GEntity(world, idNum, btTransform::getIdentity());

}

GEntity::GEntity(GWorld* world, unsigned long int idNum, btTransform trans) : m_id(idNum), m_trans(trans), m_world(world) {

// Init unused properties

m_rigidBody = NULL;

m_entity = NULL; // I'm setting it here

m_parent = NULL;

// Find internal object name

std::ostringstream ss;

ss << "Entity" << idNum << "InWorld" << world;

m_name = ss.str();

// Create a scene node

m_sceneNode = m_world->m_sceneMgr->getRootSceneNode()->createChildSceneNode(m_name+"Node");

// Initialize the SceneNode's transformation

m_sceneNode->setPosition(bv3toOv3(m_trans.getOrigin()));

m_sceneNode->setOrientation(bqToOq(m_trans.getRotation()));

}

void GEntity::setModel(std::string model) {

m_model = model;

// Delete entity on model change

if(m_entity != NULL) { // And by the time this line comes around, it's corrupt

m_world->m_sceneMgr->destroyEntity(m_entity);

m_entity = NULL;

}

// Create new entity with given model

m_entity = m_world->m_sceneMgr->createEntity(m_name+"Ent", model);

// Apply a new rigid body if needed

if(m_rigidBody != NULL) {

initPhysics();

}

}

void GEntity::initPhysics() {

deinitPhysics();

}

void GEntity::deinitPhysics() {

if(m_rigidBody != NULL) {

m_world->m_dynWorld->removeRigidBody(m_rigidBody);

delete m_rigidBody;

m_rigidBody = NULL;

}

}

以下是GEntity的定义:

class GEntity : public btMotionState {

public:

GEntity(GWorld* world, unsigned long int idNum);

GEntity(GWorld* world, unsigned long int idNum, btTransform trans);

void setModel(std::string modelName);

void initPhysics();

void deinitPhysics();

void getWorldTransform(btTransform& worldTrans) const;

void setWorldTransform(const btTransform &trans);

void parent(GEntity* parent);

protected:

unsigned long int m_id;

// Physics representation

btTransform m_trans;

btRigidBody* m_rigidBody;

// Graphics representation

Ogre::SceneNode* m_sceneNode;

Ogre::Entity* m_entity;

// Engine representation

GWorld* m_world;

GEntity* m_parent;

std::string m_name;

std::string m_model; // Used to find physics collision mesh

};

这是调用setModel的代码:

// Setup game world

GWorld* world = new GWorld(win);

GEntity* ent = world->createEntity();

ent->setModel(std::string("Cube.mesh"));

解决方法:

你的问题是这一行是在构造函数体内为不同的GEntity构建一个无名的临时GEntity.一旦语句完成,临时就被丢弃,并且不执行非临时GEntity的进一步初始化.

GEntity(world, idNum, btTransform::getIdentity());

如果要在两个构造函数之间共享一些初始化代码,则应创建一个执行所需操作的成员函数,并从两个构造函数中调用此函数. C(当前)不允许您将初始化从一个构造函数委托给另一个构造函数,或者在同一个对象上调用两个构造函数.

标签:memory-corruption,c,string,macos

来源: https://codeday.me/bug/20190726/1546610.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值