c++macosopenglsdl-2folly

SDL2 program linked against jemalloc crashes on Mac os x but not ubuntu


I followed this tutorial and set up a very simple opengl progam using SDL2. I removed parts of it for purposes of this question. The program opens a window, sets the background color to be red, and loops until the escape key is pressed.

#include <unistd.h>
#include <string>
#include <iostream>

#include <OpenGL/gl.h>
#include <SDL2/SDL.h>

using namespace std;

void CheckSDLError(int line = -1) {
  std::string error = SDL_GetError();

  if (error != "") {
    std::cout << "SLD Error : " << error << std::endl;

    if (line != -1) {
      std::cout << "\nLine : " << line << std::endl;
    }
    SDL_ClearError();
  }
}


bool SetOpenGLAttributes() {
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

  return true;
}

SDL_Window *mainWindow;
SDL_GLContext mainContext;

bool sdlInit() {
  // Initialize SDL's Video subsystem                                            
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    std::cout << "Failed to init SDL\n";
    return false;
  }

  mainWindow = SDL_CreateWindow("SDL2 window, son", SDL_WINDOWPOS_CENTERED,
                                SDL_WINDOWPOS_CENTERED,
                                640, 640, SDL_WINDOW_OPENGL);

  if (!mainWindow) {
    std::cout << "Unable to create window\n";
    CheckSDLError(__LINE__);
    return false;
  }
  // Create our opengl context and attach it to our window                       
  mainContext = SDL_GL_CreateContext(mainWindow);
  SetOpenGLAttributes();

  // This makes our buffer swap syncronized with the monitor's vertical refresh  
  SDL_GL_SetSwapInterval(1);

  return true;
}

void mainLoop() {
  while (true) {
    usleep(40000);//40 ms = 40000 us                                             
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
      cout << "here\n";
      glClearColor(1.0, 0.0, 0.0, 1.0);
      glClear(GL_COLOR_BUFFER_BIT);
      SDL_GL_SwapWindow(mainWindow);
      if (event.type == SDL_QUIT) {
        return;
      }
      if (event.type == SDL_KEYDOWN) {
        switch (event.key.keysym.sym) {
        case SDLK_ESCAPE:
          return;
          break;
        }
      }
    }
  }
}


int main(int argc, char** argv) {
  if (!sdlInit()) {
    return -1;
  }
  mainLoop();
  SDL_GL_DeleteContext(mainContext);
  SDL_DestroyWindow(mainWindow);
  SDL_Quit();
  return 0;
}

The program compiles, links and works as I'd expect when I compiles with clang on my mac with

g++ -g -std=c++0x -Wall main.cpp -o prog -framework opengl -framework SDL2

However, if I try to link against Facebook's Folly, like this:

g++ -g -std=c++0x -Wall main.cpp -o prog -framework opengl -framework SDL2 -lfolly

the program still compiles and links successfully. However, when I run the program, it crashes every time after a few seconds. lldb shows the following:

Process 36200 stopped
* thread #4: tid = 0xf254f, 0x00007fff9c6891f4 libsystem_platform.dylib`OSSpinLockLock + 7, stop reason = EXC_BAD_ACCESS (code=1, address=0x390)
    frame #0: 0x00007fff9c6891f4 libsystem_platform.dylib`OSSpinLockLock + 7
libsystem_platform.dylib`OSSpinLockLock:
->  0x7fff9c6891f4 <+7>:  lock   
    0x7fff9c6891f5 <+8>:  cmpxchgl %ecx, (%rdi)
    0x7fff9c6891f8 <+11>: je     0x7fff9c6891ff            ; <+18>
    0x7fff9c6891fa <+13>: jmp    0x7fff9c689d6e            ; _OSSpinLockLockSlow

My vague understanding is that SDL2 is dynamically linked and after reading this answer, I assume that when I link against folly, I'm using a different implementation of some library than when I don't link against folly. I'm also not sure if it's relevant, but I was able to compile the code above and link against folly on my Ubuntu desktop, and the resulting binary can be run without any problems. What do I need to do in order to use SDL2 and folly together on Mac 10.11.5?


Solution

  • An occasional SDL2 contributor here:

    I'm not seeing a crash when I run this, despite linking with Folly, and when using OSX 10.11.5.

    A few observations:

    1. the 'brew install SDL2' command didn't install an SDL2 framework. It look install a static lib, libSDL2.a, and a dynamic lib, libSDL2.dylib, into /usr/local, but no .framework. Might you be using an SDL2.framework from a different (non-brew) install?

    2. I had to add in a -I/usr/local/include and -L/usr/local/lib to the g++ call, in order to build. Might headers and/or libraries be getting retrieved from elsewhere (such as a separate, SDL2 framework)?

    3. The copy of 'g++' I had on my system was, in reality, clang, which was provided by Xcode v7.3.1, and which I presume Apple had just linked to /usr/bin/g++. Are you using an actual version of g++, and if so, what version and from what source?

    None of these are to say that they're causing the error, I'm mainly looking to see what's different about my dev environment, and yours. :-)