gpt4 book ai didi

c - 随机双自由或腐败(出)C

转载 作者:行者123 更新时间:2023-11-30 21:02:10 30 4
gpt4 key购买 nike

我面临着一个让我抓狂的问题!

我有一个函数,这个:

void    load_weapons3(t_env *e, char *name, int x, t_weapon *w)
{
char *tmp;
char *fname;
t_image i;

fname = NULL;
tmp = NULL;
tmp = ft_get_name_without_extention(name);
if (!tmp)
return ;
fname = ft_strcat(tmp, "_fire.xpm");
free(tmp);
if (!fname)
return ;
i.image = mlx_xpm_file_to_image(e->mlx_ptr, fname, &(i.x), &(i.y));
if (!i.image)
{
(*w).fire = NULL;
return ;
}
else
(*w).fire = malloc(sizeof(t_weaponfire) * QTY_OF_FIRE);
i.image_data = mlx_get_data_addr(i.image,
&(i.bpp),
&(i.size_line),
&(i.endian));
i.image_tab = get_image_tab(i);
load_weapon_fire(e, x, i);
printf("%s\n", fname);
free(fname);
}

可能相关的代码的其他部分:

int     ft_strlen(char *str)
{
int i;

i = 0;
while (str[i])
i++;
return (i);
}

char *ft_strcpy(char *str)
{
int i;
int j;
char *cpystr;

j = 0;
i = ft_strlen(str);
cpystr = malloc(sizeof(char) * (i + 1));
while (j < i)
{
cpystr[j] = str[j];
j++;
}
cpystr[j] = '\0';
return (cpystr);
}
char *ft_get_name_without_extention(char *fullpath)
{
char *str;
int i;

i = ft_strlen(fullpath);
str = ft_strcpy(fullpath);
while (i)
{
if (str[i] == '.')
{
str[i] = '\0';
return (str);
}
i--;
}
free(str);
return (NULL);
}

char *ft_strcat(char *str1, char *str2)
{
int i;
int len1;
int len2;
char *str;

i = 0;
str = NULL;
if (!str1 || !str2)
return (NULL);
len1 = ft_strlen(str1);
len2 = ft_strlen(str2);
str = malloc(sizeof(char) * (len1 + len2 + 1));
if (!str)
return (NULL);
while (i < len1)
str[i] = str1[i++];
len1 = 0;
while (len1 < len2)
str[i + len1] = str2[len1++];
str[i + len1] = '\0';
return (str);
}

void load_weapons(t_env *e)
{
int xpm_q;
DIR *d;
struct dirent *dir;

xpm_q = ft_get_xpm_quantity("img/weapons");
printf("Xpm_q is : %d\n", xpm_q);
if (xpm_q > 0)
{
e->weapons.weapons_count = xpm_q;
e->weapons.weapons = malloc(sizeof(t_image) * (xpm_q + 1));
xpm_q--;
d = opendir("img/weapons");
if (d)
{
while ((dir = readdir(d)) != NULL)
{
load_weapons2(&xpm_q, &(e->weapons.weapons[xpm_q]), e, dir->d_name);
}
closedir(d);
}
}
e->weapons.selected_weapon = 0;
}

void load_weapons2(int *xpm_quantity, t_weapon *w, t_env *e, char *n)
{
char *fname;
t_image *i;

if (!ft_have_extension(".xpm\0", n) || ft_have_extension("_fire.xpm\0", n))
return ;
i = &(w->image);
fname = ft_strcat("img/weapons/", n);
i->name = ft_strcpy(n);
i->image = mlx_xpm_file_to_image(e->mlx_ptr, fname, &(i->x), &(i->y));
i->image_data = mlx_get_data_addr(i->image,
&(i->bpp),
&(i->size_line),
&(i->endian));
i->image_tab = get_image_tab((*i));
load_weapons3(e, fname, *xpm_quantity, w);
free(fname);
(*xpm_quantity)--;
}
有时(非常随机)我会得到“双重释放或损坏(出)”,这似乎是在我释放 fname 指针时发生的。事实是我没有双重释放它,并且 printf 打印它没有任何问题......

如果有人有线索......

我使用的是 gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4,在 VirtualBox 中运行。

感谢您的阅读!

最佳答案

你的代码很糟糕,而且你还没有发布你的 typedefstruct 定义,这些定义将在以下咆哮中变得相关:

因此,在 load_weapons() 中,您 malloc() 是一个数组,

e->weapons.weapons = malloc(sizeof(t_image) * (xpm_q + 1));

其内容大概应该是t_image类型。然后将指向数组中倒数第二个有效对象的指针传递给 load_weapons2() (很棒的描述性名称),

load_weapons2(&xpm_q, &(e->weapons.weapons[xpm_q]), e, dir->d_name);

但是等等! load_weapon2() 的原型(prototype)又是什么?

void load_weapons2(int *, t_weapon *, t_env *, char *)

那不是t_image*,那是t_weapon*!令人震惊的是,您然后以某种方式t_weapon* 中提取了一个 t_image*,而这实际上是一个 t_image*首先,

t_image *i;
i = &(w->image);

最后一行有意义的唯一方法是如果t_weapon有一个成员t_image,这显然需要sizeof(t_weapon) >= sizeof(t_image)。因此,除非 t_imaget_weapon唯一成员,否则您分配的空间不足。

现在有一些完全主动提出的建议:完全重写

关于c - 随机双自由或腐败(出)C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186905/

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