stetre / moonglfw

Lua bindings for GLFW
Other
67 stars 13 forks source link

segmentation fault #10

Closed AsynchronousAI closed 1 year ago

AsynchronousAI commented 1 year ago

I am using an x86_64 MacBook Pro, I tried to see if the problem was with dlopen but it wasn't. It happens upon requiring moonglfw.

AsynchronousAI commented 1 year ago

I tried to do some tests and this is what I changed luaopen_moonglfx

#include <stdio.h>

LUAMOD_API int luaopen_moonglfw(lua_State *L)
/* Lua calls this function to load the module */
    {
    printf("opening moonglfw\n");
    moonglfw_L = L;

    moonglfw_utils_init(L);

    printf("posting enums and getproc\n");
    lua_newtable(L); /* the glfw table */
    moonglfw_open_enums(L);
    moonglfw_open_getproc(L);
    AddVersions(L);

    /* Do not include hats in glfwGetJoystickButtons() if version >= 3.3.0 */
    printf("glfwInitHint\n");
    if(checkminversion(3, 3, 0))
        glfw.InitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE);

    if(glfw.Init() != GL_TRUE)
        {
        const char *descr;
        glfw.GetError(&descr);
        if(descr) return luaL_error(L, descr);
        return luaL_error(L, "glfwInit() failed");
        }
    atexit(AtExit);
    glfw.SetErrorCallback(errorCallback);

    /* add glfw functions: */
    printf("adding functions\n");
    luaL_setfuncs(L, Functions, 0);
    printf("adding modules\n");
    moonglfw_open_window(L); printf("window\n");
    moonglfw_open_hint(L); printf("hint\n");
    moonglfw_open_monitor(L); printf("monitor\n");
    moonglfw_open_callbacks(L); printf("callbacks\n");
    moonglfw_open_input(L); printf("input\n"); 
    moonglfw_open_context(L); printf("context\n");
    moonglfw_open_vulkan(L); printf("vulkan\n");
    moonglfw_open_native(L); printf("native\n");

    return 1;
    }

It prints everything including "native" so idk where the issue is.

AsynchronousAI commented 1 year ago

I did some more debugging this is causing the error:

if(checkminversion(3, 3, 0))
        glfw.InitHint(GLFW_JOYSTICK_HAT_BUTTONS, GLFW_FALSE);

    if(glfw.Init() != GL_TRUE)
        {
        const char *descr;
        glfw.GetError(&descr);
        if(descr) return luaL_error(L, descr);
        return luaL_error(L, "glfwInit() failed");
        }
    atexit(AtExit);
    glfw.SetErrorCallback(errorCallback);
AsynchronousAI commented 1 year ago

I did even more debugging and the error is in glfw.Init

stetre commented 1 year ago

Is the segmentation fault within the glfw.Init call, or in the error section that follows?

(Unfortunately I don't have a MacOS system so I can't try it myself)

AsynchronousAI commented 1 year ago

Is the segmentation fault within the glfw.Init call, or in the error section that follows?

(Unfortunately I don't have a MacOS system so I can't try it myself)

I tried recompiling with the error section commented and just a printf("fail"). It still created a segmentation fault.

stetre commented 1 year ago

Ok, then the segv is likely occurring within glfw.Init, unless for some reason the function pointer itself wasn't loaded properly.

First of all, did the build process go smoothly, with the -DMACOS in place?

AsynchronousAI commented 1 year ago

Yes, Building went well without any errors. The glfw.dylib is located in the cwd and I got it from the official website for the correct architecture.

AsynchronousAI commented 1 year ago

unless for some reason the function pointer itself wasn't loaded properly.

This appears to be the error, If I am correct the Init function is in getproc.c?

stetre commented 1 year ago

The function pointer stored in glfw.Init is loaded in getproc.c at line 95, using the dlsym function (GET macro defined at line 69). dlsym should return either the valid pointer to glfwInit, or NULL on error. The latter case is guarded against, so either dlsym returns garbage (is it likely? I doubt), or the segv is actually within the glfwInit() call.

AsynchronousAI commented 1 year ago

The problem is inside the glfw.Init function:

if(glfw.Init)
        printf("GLFW version: %s\n", glfw.GetVersionString());
else
        return luaL_error(L, "failed to load the GLFW library");

Outputs GLFW version: 3.3.8 Cocoa NSGL EGL OSMesa dynamic and then Segmentation fault: 11.

I added the code above the glfw.Init call in main.c

stetre commented 1 year ago

This just shows that glfw.Init is not NULL, which was already guarded against, but it does not guarantee that it is a meaningful function pointer.

Anyway, this puts the segv out of my reach, unfortunately. What I would do now is to try installing GLFW using brew (as opposed to using the downloaded glfw.dylib), following these instructions kindly provided by another MacOS user.

AsynchronousAI commented 1 year ago

Thanks for help.