Reputation: 15
I’m encountering an issue where I get a Valgrind error : Conditional jump or move depends on uninitialised value(s). I am using a custom draw_sprite_to_buffer function that copies pixel data from a sprite’s buffer to my main buffer.
Here’s the relevant part of my code:
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++;
}
}
Where i initialize my sprites :
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);
}
==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)
I tried searching for posts with similar problems, but each solution I found was different.
Thanks for taking the time to read this!
EDIT : I changed my new_sprite function so it free when needed. EDIT : DELETED
Upvotes: 1
Views: 73