본문 바로가기
C | C++

[0423] C 에러 해결

by saniii 2022. 4. 23.

 

 

1.

에러명  : argument of type "t_img" is incompatible with parameter of type "void *"

-> 구조체에 t_img를 포인터형으로 선언하지 않아서 발생

void free_all(t_game *game)
{
	free(game->img);
	free(game)
}​

 

 

 

2.  이것도 img의 포인터 선언문제 

이것도 img의 포인터 선언문제, 포인터로 선언했으면 . 을 ->으로 바꿔야한다. 

해결

 

 

 

3. 메모리 누수

gnl로 한줄씩 읽어올때 이전에 저장하고 있던 라인들을 해제하지 않아서 메모리 누수 발생

int	map_read_in_line_util(t_game *game, int fd, char *line, char *temp)
{
	while (line)
	{
		game->map.col++;
		line = get_next_line(fd);
		if (line)
		{
			game->mapl = so_strjoin(temp, line);
			free(temp);
			temp = game->mapl;
			if (game->map.row != so_strlen(line))
			{	
				free(line);
				print_err(2, game);
			}
		}
		free(line);
	}
	return (fd);
}

void	map_read_in_line(char *filename, t_game *game)
{
	int		fd;
	char	*line;
	char	*mapl_temp;

	fd = open(filename, O_RDONLY);
	if (fd <= 0)
	{
		close(fd);
		print_err(1, game);
	}
	line = get_next_line(fd);
	game->map.row = ft_strlen(line) - 1;
	game->mapl = so_strdup(line);
	mapl_temp = game->mapl;
	free(line);
	fd = map_read_in_line_util(game, fd, line, mapl_temp);
	close(fd);
}

 

 

 

 

 

 

 

 

4. 프로그램 종료할 때 사용했던 포인터 모두 free하지 않으면 메모리누수 난다. 

당연한 말이긴 한데...ㅋㅎㅋㅎㅋㅋㅋ

void	print_err(int err_num, t_game *game)
{
	write(2, "Error\n", 6);
	if (err_num == 1)
		write(2, "FAIL open file\n", 15);
	else
	{
		if (err_num == 2)
			write(2, "map is NOT rectangular\n", 23);
		else if (err_num == 3)
			write(2, "map is NOT surrounded by wall\n", 30);
		else if (err_num == 4)
			write(2, "There is NO exit\n", 17);
		else if (err_num == 5)
			write(2, "There is NO colloectible\n", 25);
		else if (err_num == 6)
			write(2, "There are TOO MANY or NO start point\n", 37);
		else if (err_num == 7)
			write(2, "There is wrong param in mapfile.\n", 33);
		free(game->mapl);
	}
	exit_game_by_error(game);
}

int	exit_game(t_game *game)
{
	mlx_destroy_window(game->mlx, game->win);
	free(game->mapl);
	free(game);
	system("leaks so_long");
	exit(0);
}

void	exit_game_by_error(t_game *game)
{
	free(game->mlx);
	free(game);
	system("leaks so_long");
	exit(1);
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

'C | C++' 카테고리의 다른 글

<pthread.h>  (0) 2022.04.30
open | close | read | write (저수준 파일입출력 함수)  (0) 2022.04.24
Makefile  (0) 2022.04.12
[ C ] 동적할당 malloc(), free()  (0) 2021.12.29
[C++] c++ 이 뭔가요?  (0) 2021.10.20

댓글