codam-coding-college/MLX42

[BUG] mlx_terminate segfaults

GroteGnoom opened this issue · 5 comments

NOTE
Before creating a bug report! Make sure you git pull from master and check if the bug still exists!

Describe the bug
mlx_terminate segfaults with minimal program

To Reproduce

# include "MLX42/MLX42.h"
# include <stdio.h>
# include <stdlib.h>

# define WIDTH 512
# define HEIGHT 512

int    main(void)
{
    mlx_t    *mlx;

    if (!(mlx = mlx_init(WIDTH, HEIGHT, "MLX42", true)))
    {
        puts(mlx_strerror(mlx_errno));
        return(EXIT_FAILURE);
    }
}
FROM ubuntu:latest

RUN apt-get update && apt-get install -y \
    gcc \
    g++ \
    cmake \
    libglfw3 \
    libglfw3-dev \
    libgl1-mesa-dev \
    xorg \
    mesa-utils \
    git \
    libgl1-mesa-glx \
    libgl1-mesa-dri \


ENV DISPLAY=:0

RUN git clone https://github.com/codam-coding-college/MLX42.git /MLX42

COPY minilibx.c /tmp

WORKDIR /tmp

RUN cd /MLX42 && cmake -B build -DCMAKE_BUILD_TYPE=Debug && cmake --build build -j4

CMD gcc minilibx.c -g /MLX42/build/libmlx42.a -I/MLX42/include -ldl -lglfw -pthread -lm && ./a.out

Expected behavior
Either open a window and quickly close it, or terminate with an error

Desktop (please complete the following information):

  • OS: ubuntu
  • Version: 22.04

Additional context
I first had these errors on our linux cluster computers:

f0r3s1% gcc mlxtest.c ../MLX42/build/libmlx42.a -I../MLX42/include
-ldl -lglfw -pthread -lm
f0r3s1% ./a.out
zsh: segmentation fault (core dumped)  ./a.out
f0r3s1% ./a.out
LLVM ERROR: Do not know how to split the result of this operator!

zsh: IOT instruction (core dumped)  ./a.out
f0r3s1% ./a.out
LLVM ERROR: Do not know how to split the result of this operator!

zsh: IOT instruction (core dumped)  ./a.out
LLVM ERROR: Invalid size request on a scalable vector.
free(): invalid pointer
fish: Job 1, './a.out' terminated by signal SIGABRT (Abort)

all on the same code. I tried to find the issue with address sanitizer, but then the bug goes away and never comes back, even without the sanitizer. So then I couldn't reproduce it anymore and I tried a docker. This segfault could be the issue. Maybe there are multiple issues.

Well that's a whole new error i've never seen before.
I however simply get:

- docker build -t mlx42-test .
- docker run -it mlx42-test
Failed to initialize GLFW

Which makes sense as there is no display to launch with docker, even with MLX_HEADLESS it won't work as a "Window" context is needed.

Yes, there was a lot of work to overcome 'Failed to initialize GLFW' for me too :D I used xquartz and

echo 'first start docker desktop yourself, else this will not work'
export DISPLAY=127.0.0.1:0
xhost + 127.0.0.1
export BUILDKIT_PROGRESS=plain
docker build --build-arg CACHEBUST=$(date +%s) -t mlx42-app .
docker run --rm -it -e DISPLAY=host.docker.internal:0 \
     -e LIBGL_ALWAYS_SOFTWARE=1 \
     -e LIBGL_ALWAYS_INDIRECT=1 \
     -v /tmp/.X11-unix:/tmp/.X11-unix mlx42-app
  • not everything is relevant, but maybe this works

Ok so this took a bit of digging but I found a docker file that handles exactly all of this stuff.

I suggest forking: https://github.com/BoundfoxStudios/docker-opengl

FROM boundfoxstudios/opengl:latest

# Install necessary dependencies
RUN apk update && apk add --no-cache gcc g++ make openssl-dev cmake git bash glfw-dev mesa-dev git

# Clone the repo
RUN git clone https://github.com/codam-coding-college/MLX42.git /MLX42

# Copy your minilibx.c file
COPY minilibx.c /tmp

# Set the working directory
WORKDIR /tmp

# Build the project
RUN cd /MLX42 && \
    cmake -B build && \
    cmake --build build -j4

# Set the default command to compile and run your program
CMD gcc /tmp/minilibx.c -g /MLX42/build/libmlx42.a -I/MLX42/include -ldl -lglfw -pthread -lm && ./a.out

I modified the .c file to just print the address:

# include "MLX42/MLX42.h"
# include <stdio.h>
# include <stdlib.h>

# define WIDTH 512
# define HEIGHT 512

int    main(void)
{
    mlx_t    *mlx;

    mlx_set_setting(MLX_HEADLESS, true);
    if (!(mlx = mlx_init(WIDTH, HEIGHT, "MLX42", true)))
    {
        puts(mlx_strerror(mlx_errno));
        return(EXIT_FAILURE);
    }
    printf("mlx_init: %p\n", mlx);
}

And instead of an error now you get:

Starting Xvfb
Waiting for Xvfb (PID: 7) to be ready...
Xvfb is running.
mlx_init: 0xffff915dd1b0
Waiting for Xvfb (PID: 7) to shut down...

I tested it with the program in the README.md, seems to work perfectly fine!

If this works for you too i'll add it to the readme or create a Docker example so in the future others can look into how to make it work with Docker and avoid doing all this annoying Xvfb non-sense.

I assume for now, yes, if not, please re-open.