gpt4 book ai didi

c++ - 奇怪的段错误 - 将指针指向 vector 的对象推回导致崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 12:03:31 26 4
gpt4 key购买 nike

我正在尝试使用 SDL 编写游戏,但遇到了很多奇怪的段错误。

我创建了类 Monster 和 Player,它们从类 Creature 获取公共(public)变量。 Creature 类从 Object 类获取变量。就这样:

class Object {
public:
Area* area_pointer; // Pointer to an Area in which object is present.
Sprite sprite; // Class that has SDL_Surface* and some other things.
Animation animation; // nothing usefull
int ID;
short int position_x; // nothing usefull
short int position_y; // nothing usefull
short int effect_x; // nothing usefull
short int effect_y; // nothing usefull
.... //Some functions that are not important right now.
};

Sprite 类:

class Sprite {
public:
SDL_Surface* image;
short int x;
short int y;
short int w;
short int h;
Sprite ()
{
image = NULL;
x = 0;
y = 0;
w = 0;
h = 0;
}
};

类生物、玩家、怪物:

class Creature : public Object {
public:
char move_direction;
char speed;
..// Some not important functions.
};
class Player : public Creature {
public:
char select_type;
int select_ID;
std::vector <Item> equip;
std::vector <Item> inventory;
std::vector <Effect> effects;
Player (Area* c_area_pointer, int data [])
{
area_pointer = c_area_pointer;
sprite.image = SurfaceLoad ("Images/Players/" + IntToString (data [0]) + ".png");
sprite.x = 0;
sprite.y = 0;
sprite.w = 0;
sprite.h = 0;
ID = data [0];
position_x = data [1];
position_y = data [2];
effect_x = 0;
effect_y = 0;
}
~Player ()
{
SDL_FreeSurface (sprite.image);
.....
}
};
class Monster : public Creature {
public:
char type;
std::vector <Item> loot;
std::vector <Effect> effects;
Monster (Area* c_area_pointer, int data [])
{
area_pointer = c_area_pointer;
sprite.image = SurfaceLoad ("Images/Monsters/" + IntToString (data [3]) + ".png");
sprite.x = 0;
sprite.y = 0;
sprite.w = 0;
sprite.h = 0;
ID = data [0];
position_x = data [1];
position_y = data [2];
effect_x = 0;
effect_y = 0;
type = data [3];
}
~Monster ()
{
SDL_FreeSurface (sprite.image);
.....
}
};

表面加载函数:

SDL_Surface* SurfaceLoad (std::string file_name)
{
SDL_Surface* surface_1 = NULL;
SDL_Surface* surface_2 = NULL;
surface_1 = IMG_Load (file_name.c_str ());
surface_2 = SDL_DisplayFormat (surface_1);
if (surface_1 != surface_2) SDL_FreeSurface (surface_1); // This line may be strange to some of you, but it is related to other error i had in past.
SDL_SetColorKey (surface_2, SDL_SRCCOLORKEY, 0xFFFFFF);
return surface_2;
}

我尝试从 txt 文件加载数据并基于它制作对象。我为此使用了自己的 Load 函数。它创建一个指向类对象的指针,例如:

        Player* player;
std::string players_name = save_name + "Areas/" + IntToString (ID) + "/players.txt"; //Path to the file cointaining players data.
std::ifstream players_file;
players_file.open (players_name.c_str ());
Monster* monster;
std::string monsters_name = save_name + "Areas/" + IntToString (ID) + "/monsters.txt"; //Path to the file cointaining monsters data.
std::ifstream monsters_file;
monsters_file.open (monsters_name.c_str ());

然后它从文本文件中加载数据并将其放入名为 file_data 的 int 数组中,并基于它创建新的类对象。

        while (!players_file.eof ())
{
getline (players_file, file_text);
while (file_text [data_position_2 + 1] != ';')
{
data_position_2 = file_text.find (",", data_position);
data.assign (file_text, data_position, data_position_2 - data_position);
file_data [data_index] = atoi (data.c_str ());
data_position = data_position_2 + 1;
data_index++;
}
player = new Player (this, file_data);
this->area_map.players.push_back (*player); //Vector players inside object area_map which contain also monsters vector. "this" is a pointer to object that contain Load function and area_map object.
delete player;
data_index = 0;
data_position = 0;
data_position_2 = 0;
}

这部分代码可以工作,但是对 monsters_file 和 monsters vector 执行完全相同的操作会导致很多奇怪的错误。我得到的第一个错误是在将数据指针推送到 vector 后删除指针时出现段错误。我检查了一下,发现程序在调用SDL_FreeSurface()时在deconstructor中崩溃(segfault)。所以我检查了我的构造函数是否正确加载表面。我发现构造一个对象一切正常,但是在调用 SurfaceLoad() 时突然开始崩溃(段错误)。也检查了这个函数,一切正常:指向表面的指针正常,它返回的指针正常,但由于某种原因它崩溃了:

sprite.image = SurfaceLoad (...);

一段时间后它停止在这里崩溃,没有任何原因(我只是添加了那行

if (surface_1 != surface_2) SDL_FreeSurface (surface_1);

,因为我注意到 SDL_DisplayFormat () 有时会返回与指向未格式化表面的指针相同的指针。)并且当我将指针指向 vector 的 push_back 对象指向时开始崩溃(也是段错误):

this->area_map.monsters.push_back (*monster);

Monster 和 Player 类在创建游戏的这个阶段几乎相同,所以我不知道为什么它创建 player 没有任何问题,而创建 monster 却有这么多问题。有人知道如何解决这个问题吗?

最佳答案

大量的代码看起来有点杂乱无章。例如,为什么 Player 析构函数要释放在 Sprite 中声明的图像?这不应该是 Sprite 析构函数的工作吗?为什么一切都是公开的?但无论如何,从你描述的症状来看,这听起来像是一个未能遵循三法则的经典案例。

当您编写在内部分配内存或其他资源的类(如 Sprite 中的图像指针)时,您必须编写正确处理分配的内存或资源的复制构造函数和赋值运算符。如果你不这样做,你会得到这样的错误。

参见 here获取有关三法则的一些基本信息。如果您不理解这些东西,那么您将永远在编写有缺陷的 C++ 代码。有关管理资源的部分与您最相关,但请全部阅读。

关于c++ - 奇怪的段错误 - 将指针指向 vector 的对象推回导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13109722/

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