Sign in to follow this  

Problems with Boost.Bind and Boost.Function

This topic is 2664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi

I've been trying to pass a pointer to a member function as a callback argument and after countless hours of reading on the internet I finally found a possible solution. Using Boost.Bind and Boost.Function I can get my app to at least compile :), but then it crashes :( right where I call the target() member of Boost.Function. I get this message from Visual Studio: "Unhandled exception at 0x01299ba4 in MD5_Loader.exe: 0xC0000005: Access violation reading location 0x00000000."

A bit of background: I'm using GLFW and trying to register a member of my Window class with glfwSetWindowSizeCallback(), as all of you probably know, this is quite a mission.

This is the code for constructor.


Window::Window(const int &width, const int &height, bool fullscreen)
{
// Initialise GLFW.
if (glfwInit() != GL_TRUE)
{
throw std::runtime_error("Cannot initialise GLFW");
}

// Create the window.
if (!glfwOpenWindow(width, height, 0, 0, 0, 0, 0, 0, GLFW_WINDOW))
{
glfwTerminate();
throw std::runtime_error("Could not create GLFW Window.");
}


// All to pass a member function as a C style callback function.

boost::function<void GLFWCALL (int, int)> f = boost::bind(&Window::resize_callback, this, _1, _2);
glfwSetWindowSizeCallback(*f.target<GLFWwindowsizefun>());
}



And this is the (private) member function that I'm passing. I'm just testing if it works at the moment.

void GLFWCALL Window::resize_callback(int width, int height)
{
std::cout << "Resized the window to: " << width << ", " << height << std::endl;
}



I'm just trying to figure out why it's crashing and if I'm using Bind and Function correctly?

Share this post


Link to post
Share on other sites
Well, you certainly get points for creativity in trying to coerce GLFW into accepting a generalized function object :) But yeah, that's not going to work.

Generally (I think) you don't need or want to be dealing with function::target(). In the docs, it states that if the target function object is not of the requested type (GLFWwindowsizefun in this case), it returns null. I think if you split that up into a couple of statements and add some error-checking, you'll see that target() is returning a null pointer. (The crash probably occurs when you try to dereference it.)

Unfortunately, I don't think Function or Bind is going to help you here (at least not directly). GLFW wants a C-style function pointer, period, and there's really no way to (successfully) convince it to accept a member function pointer (this is a problem that comes up frequently on the forums, actually).

target() won't return a GLFWwindowsizefun*; the object that the function instance holds doesn't have anything to do with GLFWwindowsizefun. In fact, the target probably isn't even a member function pointer, but rather some complex type generated by the Bind library (in other words, it's a function object targeting the specified member function, bound to the specified instance, and taking two arguments).

So in summary, I think you'll have to find another way to get the callback routed to your window object. Again, this has been discussed fairly frequently in the past, so if you search the forum archives for (e.g.) 'callback member function', you should be able to get some ideas about how to handle this.

Also, this is unrelated, but there's probably not much to be gained by passing ints by constant reference; you can just pass them by value.

Share this post


Link to post
Share on other sites

This topic is 2664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this