t_sprite *new_sprite(void *mlx, char *filepath)
{
t_sprite *tmp;
tmp = malloc(sizeof(t_sprite));
if (!tmp)
return (NULL);
tmp->img_ptr = mlx_xpm_file_to_image(mlx, filepath, &tmp->width, &tmp->height);
if (!tmp->img_ptr)
return (NULL);
tmp->buffer = mlx_get_data_addr(tmp->img_ptr, &tmp->bpp, &tmp->line_len, &tmp->endian);
if (!tmp->buffer)
return (NULL);
return (tmp);
}
==73326== Conditional jump or move depends on uninitialised value(s)
==73326== at 0x10F294: draw_sprite_to_buffer (sprite.c:34)
==73326== by 0x10F1C8: render (render.c:55)
==73326== by 0x10EB28: game_loop (game.c:45)
==73326== by 0x110494: mlx_loop (in /home/nahil/42Cursus/so_long/game_test)
==73326== by 0x10E9D7: main (main.c:33)
==73326== Uninitialised value was created by a heap allocation
==73326== at 0x4846828: malloc (in /usr/libexec/valgrind/vgpreload_memcheck-amd64-linux.so)
==73326== by 0x111152: mlx_int_parse_xpm (in /home/nahil/42Cursus/so_long/game_test)
==73326== by 0x1117F3: mlx_xpm_file_to_image (in /home/nahil/42Cursus/so_long/game_test)
==73326== by 0x10F360: new_sprite (sprite.c:53)
==73326== by 0x10EC1E: init_player (player.c:23)
==73326== by 0x10EE11: init_level (level.c:63)
==73326== by 0x10EB57: init_game (game.c:52)
==73326== by 0x10E9CB: main (main.c:32)
我尝试搜索具有类似问题的帖子,但是我发现的每个解决方案都不同。 thanks花时间阅读此书!
这里是关键修复的简明摘要:fixes for
Valgrind: Conditional jump or move depends on uninitialized value(s)
估算缓冲液在
if (!tmp->buffer) {
mlx_destroy_image(mlx, tmp->img_ptr);
free(tmp);
return (NULL);
}
draw_sprite_to_buffer
中检查:
if ((x + x_offset) < 0 || (x + x_offset) >= buffer->width ||
(y + y_offset) < 0 || (y + y_offset) >= buffer->height) {
continue;
}
确保正确处理透明度颜色(
0xFF000000
)。测试在提取之前是否有效。
color
错误的错误记忆: 释放
sprite->buffer
if (!sprite->buffer) {
printf("Buffer not initialized!\n");
}
未能避免泄漏时销毁。