Valgrind报错:条件跳转依赖未初始化值问题求助
Valgrind未初始化值错误排查:draw_sprite_to_buffer函数问题
在使用自定义draw_sprite_to_buffer函数将精灵像素数据复制到主缓冲区时,触发Valgrind错误:Conditional jump or move depends on uninitialised value(s)。
相关函数代码
draw_sprite_to_buffer函数
void draw_sprite_to_buffer(t_sprite *buffer, t_sprite *sprite, int x_offset, int y_offset) { int y = 0; int x = 0; char *src_pixel; unsigned int color; char *dst_pixel; if (!buffer->buffer || !sprite->buffer) return ; while (y < sprite->height) { x = 0; while (x < sprite->width) { src_pixel = sprite->buffer + (y * sprite->line_len + x * (sprite->bpp / 8)); color = *(unsigned int *)src_pixel; if (color != 0xFF000000) { dst_pixel = buffer->buffer + ((y + y_offset) * buffer->line_len + (x + x_offset) * (buffer->bpp / 8)); *(unsigned int *)dst_pixel = color; } x++; } y++; } }
精灵初始化函数new_sprite
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) { free(tmp); return (NULL); } tmp->buffer = mlx_get_data_addr(tmp->img_ptr, &tmp->bpp, &tmp->line_len, &tmp->endian); if (!tmp->buffer) { mlx_destroy_image(mlx, tmp->img_ptr); free(tmp); return (NULL); } return (tmp); }
Valgrind报错日志
==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)
补充说明
已搜索同类问题,但解决方案各不相同,特此寻求帮助。
编辑说明:已修改new_sprite函数以按需释放资源;部分编辑内容已删除。
内容的提问来源于stack exchange,提问作者B. Nahil
相关产品推荐
相关产品推荐

