Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

c - imac freezes after running program

I am doing work with the training graphics library mlx. While testing the library, I ran into the problem that this code is causing my imac mojave to freeze. Only reboot helps. What's wrong?

My code

#include "./minilibx/mlx.h"
#include <math.h>
typedef struct  s_data
{
    void        *img;
    char        *addr;
    int         bits_per_pixel;
    int         line_length;
    int         endian;
}               t_data;
void            my_mlx_pixel_put(t_data *data, int x, int y, int color)
{
    char    *dst;
    dst = data->addr + (y * data->line_length + x * (data->bits_per_pixel / 8));
    *(unsigned int*)dst = color;
}
void DrawCircle(t_data *img, int x, int y, int r)
 {
       static const double PI = 3.1415926535;
       double i, angle, x1, y1;
       for(i = 0; i < 360; i += 0.1)
       {
             angle = i;
             x1 = r * cos(angle * PI / 180);
             y1 = r * sin(angle * PI / 180);
          my_mlx_pixel_put(img, x1 + x, y1 + y, 0x00FF0000);
       }
}

int     main(void)
{
    void    *mlx;
    void    *mlx_win;
    t_data  img;
    int     x = 0, y = 0;
    mlx = mlx_init();
    mlx_win = mlx_new_window(mlx, 1920, 1080, "Hello world!");
    img.img = mlx_new_image(mlx, 1920, 1080);
    img.addr = mlx_get_data_addr(img.img, &img.bits_per_pixel, &img.line_length,
                                    &img.endian);
    DrawCircle(&img, x, y, 25);
    mlx_put_image_to_window(mlx, mlx_win, img.img, 0, 0);
    mlx_loop(mlx);
}

mlx_new_image()

void    *mlx_new_image(mlx_ptr_t *mlx_ptr, int width, int height)
{
  mlx_img_list_t        *newimg;

  //  if (mlx_ptr->win_list == NULL)
  //    return (NULL);  // need at leat one window created to have openGL context and create texture
  if ((newimg = malloc(sizeof(*newimg))) == NULL)
    return ((void *)0);
  newimg->next = mlx_ptr->img_list;
  mlx_ptr->img_list = newimg;
  newimg->width = width;
  newimg->height = height;
  newimg->vertexes[0] = 0.0;  newimg->vertexes[1] = 0.0;
  newimg->vertexes[2] = width;  newimg->vertexes[3] = 0.0;
  newimg->vertexes[4] = width;  newimg->vertexes[5] = -height;
  newimg->vertexes[6] = 0.0;  newimg->vertexes[7] = -height;
  newimg->buffer = malloc(UNIQ_BPP*width*height);
  bzero(newimg->buffer, UNIQ_BPP*width*height);

  return (newimg);
}

Everything worked fine when x and y in main were x = 1000 and y = 500. But I changed them to zeros and everything started to freeze, with no way to stop anything. I always thought that when trying to interact with an unallocated memory cell, there would be a segfault, this provides security, but I seem to be wrong

question from:https://stackoverflow.com/questions/65901935/imac-freezes-after-running-program

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...