Passing GLFWwindowsizefun into function

trulystupidnewb wrote on Wednesday, December 01, 2010:

Hello. I’m having a bit of trouble with passing a function pointer from a
function parameter into glfwSetWindowSizeCallback. I’ve been trying to do it
for about 5 hours. I’m running VC++ 2010 under both Window XP (one computer)
and Windows 7 (another computer).

I’m trying to pass this function:

// declaration
static void GLFWCALL Resize(int width, int height);

// definition
void GLFWCALL TestGame::Resize(int width, int height)
glViewport(0, 0, width, height); // Sets the viewport to fit the size of the
new window

glMatrixMode(GL_PROJECTION); // Prepare to set up the projection matrix
glLoadIdentity(); // First reset the matrix

// Then calculate new projection matrix
gluPerspective(45.0f, // angle of view
(float)width/height, // screen aspect ratio
5.0f, // near plane for drawing cutoff
600.0f); // far plane for drawing cutoff

glMatrixMode(GL_MODELVIEW); // Now switch back to the modelview matrix
glLoadIdentity(); // Reset it and we are ready to draw

through this function:

// declaration
void SetResizeFunction(void (*resizeFunction) (int, int));
void GlfwWindow::SetResizeFunction(void (*resizeFunction) (int, int))

and calling it like this:

#include “TestGame.h”
#include “GlfwWindow.h”
#include “TWInput.h”


std::auto_ptr g_glfwWindow;
std::auto_ptr g_TWInput;
std::auto_ptr g_TWGame;

int main()
g_glfwWindow = GlfwWindow::CreateGlfwWindow(“Star Sema”, 800, 600);

g_TWInput = TWInput::CreateTWInput();
g_TWGame = TestGame::CreateGame();

double deltaTime = 0.0;
while ( g_TWGame->IsRunning() )
deltaTime = g_glfwWindow->GetElapsedSeconds();


catch(std::string errorMessage)
std::cout << errorMessage << std::endl;

return 0;

The SetCloseFunction works. However, the SetResizeFunction throws “Unhandled
exception at 0x00000258 in Barebones.exe: 0xC0000005: Access violation.”

This is truly mysterious because both functions are written similarily but one
works and the other doesnt:

this works:

void GlfwWindow::SetCloseFunction(int (*closeFunction) (void))

does anyone have any suggestions on passing GLFWwindowsizefun as a param?
Thanks in advance!

trulystupidnewb wrote on Wednesday, December 01, 2010:

I did another 30 minutes of experiments. It seems that the linker is like
bugging up. Calling SetCloseFunction(width, height) works perfectly inside a
GlfwWindow (custom class) function, but somehow when I try to use it in
main, the linker sometimes cannot resolve TestGame::Resize or other random
issues. It seems like the function is passing the param correctly. Something
else is wrong. Any hints?

trulystupidnewb wrote on Wednesday, December 01, 2010:

Nevermind, I solved the issue. I had to includ glfw.lib instead of glfwdll.lib
and remove all the #define GLFW_DLL. It works perfectly now. Thanks.