gpt4 book ai didi

c++ - C++中奇怪的析构函数调用

转载 作者:行者123 更新时间:2023-11-28 03:11:13 24 4
gpt4 key购买 nike

我有那些继承类:基类:实体

派生自实体类:Actor、Obj、Enemy

基类实体包含一个用户定义类型的对象,我称之为“CollisionStuff”。当我运行我的程序时,CollisionStuff 的析构函数在每次 CollisionStuff 构造函数调用之后以及每次游戏循环继续时被调用。

所以我的想法是:为什么会这样?

正如您在下面看到的,我在 setRectangle 方法中动态分配了一些数组,程序调用析构函数,它删除了我的数据,当我尝试使用它们时...它调用“_ASSERTE(_BLOCK_TYPE_IS_VALID(pHead->nBlockUse ));"。

谢谢你在之前

这里是我的代码:Entity.h

enum e_Type {tActor = 0, tObj, tEnemy, tBackg};

class Entity
{
public:
Entity(void);
~Entity(void);

float getH();
float getW();
void setWH(float W, float H);
bool CreateSprite(std::string path);
sf::Sprite& getSprite();
void setType(e_Type type);
e_Type getType();
CollisionStuff getColStuff();

static std::list<Entity*> List;

protected:
sf::Sprite m_sprite;
sf::Texture m_texture;
float m_h;
float m_w;
e_Type m_type;
CollisionStuff m_colStuff;

void addToList();
};

CollisionStuff.h

class CollisionStuff
{
public:
CollisionStuff();
~CollisionStuff(void);

void setRectangle(int W, int H);
void followTheSprite(Entity entity);

private:
sf::Vector2f* m_a;
sf::Vector2f* m_b;
sf::Vector2f* m_c;
sf::Vector2f* m_d;
/* this member data are sides of rectangle used
to manage collisions between object throughout the scenario

a
-------------
| |
c | | d
| |
-------------
b
*/

};

CollisionStuff.cpp

CollisionStuff::CollisionStuff()
{
//setRectangle(0, 0);
}

void CollisionStuff::setRectangle(int W, int H)
{
m_a = new sf::Vector2f[W];
m_b = new sf::Vector2f[W];
m_c = new sf::Vector2f[H];
m_d = new sf::Vector2f[H];
}

void CollisionStuff::followTheSprite(Entity entity)
{
entity.getSprite().setOrigin(0, 0);
sf::Vector2f UpLeftVertex = entity.getSprite().getPosition();


for(int i = 0; i < entity.getW(); i++)
{
m_a[i].x = UpLeftVertex.x + i;
m_a[i].y = UpLeftVertex.y;

m_b[i].x = UpLeftVertex.x + i;
m_b[i].y = UpLeftVertex.y + entity.getH();
}

for(int i = 0; i < entity.getH(); i++)
{
m_c[i].x = UpLeftVertex.x;
m_c[i].y = UpLeftVertex.y + i;

m_d[i].x = UpLeftVertex.x + entity.getW();
m_d[i].y = UpLeftVertex.y + i;
}

}



CollisionStuff::~CollisionStuff(void)
{
delete [] m_a;
delete [] m_b;
delete [] m_c;
delete [] m_d;
}

编辑谢谢你的回答。CollisionStuff 使用示例

Actor.cpp(它是实体的派生类)

Actor::Actor(void)
{
if(!CreateSprite("D://Sprites//MainChar.png"))
{
std::cout << "Impossibile creare sprite" << std::endl;
}
else
{
std::cout << "Creazione sprite riuscita" << std::endl;
m_sprite.setPosition(100.0f, 365.0f);
m_sprite.setOrigin(20, 35);
//m_sprite.setPosition(190.0f, 382.5f); // 200, 400
setWH(40, 70);
m_health = 100;
m_status = Good;
setType(tActor);
m_jCounter = -1;
m_action = Null;

setColStuff();
}
}

void Actor::setColStuff()
{
m_colStuff.setRectangle(m_w, m_h);
}



void Actor::physic()
{
//setColStuff();
m_colStuff.followTheSprite(*this);
}

main.cpp

int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "Platform");

std::list<Entity*>::iterator i;

Background BG;
Level1 FirstLev;
Actor Doodle;

while(window.isOpen())
{
sf::Event event;
if(window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();

Doodle.inputEvts();
}

Doodle.act(Doodle.getAction());

Doodle.physic();

window.clear();

window.draw(BG.getSprite());
window.draw(Doodle.getSprite());

FirstLev.drawLevel(window);

window.display();
}
return 0;
}

最佳答案

从您发布的代码中很难判断,但如果我不得不猜测,我会说这可能与此有关:

CollisionStuff getColStuff();

你回来了 CollisionStuff按值,这意味着调用它的人将创建一个新拷贝。它将具有与原始 CollisionStuff 相同的指针分配的对象,当它超出范围时它将删除它们,留下带有悬空指针的原始对象。

您可以尝试通过引用或指针返回,但无论哪种方式,您都应该编写一个复制构造函数并覆盖 CollisionStuff 的赋值运算符。 (Rule of Three)。

另一个想法是使用 std::vector<sf::Vector2f>而不是分配 sf::Vector2f自己布置。

关于c++ - C++中奇怪的析构函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18419846/

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