StartRasterGL() access voilation

I am trying to use SDL2 for OpenGL interop. I find something very strange during StartRasterGL()
that I still don’t know why.

First I make and compile the gvdb library according to Readme, selected BUILD_OPENGL
and set build mode as Release.

Then I build g3print sample in Release mode. The sample works find in Release but throwing access violation when

gvdb1.getScene()->AddModel ( "lucy.obj", 1.0, 0, 0, 0 ); // Access violation here
gvdb1.CommitGeometry( 0 );					// Send the polygons to GPU as OpenGL VBO

is called. But StartRasterGL() works fine in both mode.

Then I set up my own project using SDL2 and glew 2.1.0.
First

VolumeGVDB gvdb;
float voxel_size = 0.1f;
SDL_Init(SDL_INIT_EVERYTHING);	// Initilize everything using SDL
std::string title = "test";
unsigned int width = 1920;
unsigned int height = 1080;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);	// Set attributes for SDL window
SDL_Window* window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL);	// Create OpenGL window using SDL
SDL_GLContext gl_context = SDL_GL_CreateContext(window);	// Create OpenGL context
glewInit();

Then

gvdb.SetDebug(true);
gvdb.SetVerbose(true);		// enable/disable console output from gvdb
gvdb.SetProfile(true, true);
gvdb.SetCudaDevice(GVDB_DEV_FIRST);
gvdb.Initialize();
gvdb.StartRasterGL();    // Access violation

are called. I got access violation when calling StartRasterGL() in debug mode but everything is ok in Release;

I manage to pin down the error line which is line 261 in gvdb_scene.cpp, function AddShader()

// Read shader source	
GLuint program = glCreateProgram();

This glCreateProgram() is linked to glew.h from gvdb source folder, I am not sure about the cmake process of this but I assume glew is baked into libgvdb.lib and libgvdb.dll. While my project link glew to its official binaries glew32.lib, glew32s.lib and glew32.dll.

I succeed creating a screen quad shader program so I can confirm the gl context exists, also gvdb can find glsl shader files. My initial guess is somehow gvdb_scene cannot find the gl context in debug mode. But calling

gvdb.ValidateOpenGL()

does not give me any error in debug. so I think GVDB can find the gl context. Then I think maybe it is the glew version issue so I replaced glew files in gvdb src with newly downloaded version. The same error still happens. Then I changed the cmake mode of gvdb library from Release to Debug but same error still occurs.

As an inexperienced OpenGL user, I am running out of ideas about what is going wrong here. Apparently the glCreateProgram() in AddShader() is linked to libgvdb.lib and my explicit glCreateProgram() is linked to glew32.lib or glew32s.lib. But how come StartRasterGL() works fine in Release but not in Debug?

I kinda pin down the problem. Seems like gvdb exports libgvdb.dll and .lib with the inclusion of glew(compiled from glew.c), so when I use glCreateProgram inside gvdb source code, it imports function from libgvdb.dll, but when I use glCreateProgram in my own code, it imports function from glew32.dll, apparently in debug mode, the glCreateProgram in libgvdb.dll and glew32.dll has some compatibility issues.

So my next step is probably going to replace glew.c in gvdb project with glew.dll. Somehow I got error while building saying LNK2005 glewInit already defined in glew32.lib(glew32.dll), so I need to find a way to eliminate that. I can tell its a basic problem since gvdb also uses cudpp as dll but has no issue building.

New update: seems that compared with stock glew.h from download, glew.h in sample_utils/GL of gvdb source code has extra lines of

#ifdef GLEW_MX
#pragma error "unsupported option"
#endif

#ifndef GLEW_USER_ASSERT
#define GLEW_USER_ASSERT(condition)
#endif

someone on github suggest that this will cause compatibility issue
https://github.com/nigels-com/glew/issues/71

still trying to find out a solution for this, but I think I have found the cause.

Finally I have succeed compiling GVDB library with glew32.lib and glew.dll. It is more obvious after I figured it out. just Remove glew.c from project, add glew32.lib to the linker, replace glew.h with the downloaded one from glew website. Then remove GLEW_STATIC preprocessor token in the project property.