- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我面临着一个让我抓狂的问题!
我有一个函数,这个:
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 中运行。
感谢您的阅读!
最佳答案
你的代码很糟糕,而且你还没有发布你的 typedef
和 struct
定义,这些定义将在以下咆哮中变得相关:
因此,在 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_image
是 t_weapon
的唯一成员,否则您分配的空间不足。
现在有一些完全主动提出的建议:完全重写。
关于c - 随机双自由或腐败(出)C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32186905/
一个特定文档不会更新,而同一查询会更新我查询的任何其他 _id: Category.findOneAndUpdate( {_id : req.params.id}, {parent :
我正在使用 ifstream 读取文件头。编辑:我被要求放置完整的最小程序,所以就在这里。 #include #include using namespace std; #pragma pack(
typedef struct { int *info; } row; struct { row* head; int len; int size; } list; in
在索引 block 时我在 debug.log 中有错误: 2016-11-14 15:14:52 UpdateTip: new best=000000000000038ad111e773453f17
我是一名优秀的程序员,十分优秀!