Shared library string safety?

Mike Parker aldacron at gmail.com
Tue Jan 14 04:55:02 PST 2014


On 1/14/2014 9:10 PM, Mineko wrote:

>
> Perhaps I should be using () with GLFW3.load and glfwInit?
>

Yes, please do this. For DerelictGLFW3.load, it's just a matter of style 
and doesn't make a difference. However, glfwInit is a *function 
pointer*, not a function.. So what you're effectively doing there is 
testing if the function pointer is null or not. You aren't actually 
calling the function. Since you already called DerelictGLFW3.load, then 
it obviously isn't null, so your method completes and returns true. Then 
later, when you call glfwCreateWindow, since the library was never 
initialized, it returns null.

You could have saved yourself a lot of time by using an error callback 
as recommended earlier in this thread. And you should set the callback 
between DerelictGLFW3.load and glfwInit.

extern(C) nothrow void onErr(int code, const(char)* msg)
{
   // log the message, but be aware that you may need to wrap
   // it in a try...catch block, since the callback has to be
   // nothrow.
}

auto initGLFW()
{
     import breaker.main : timer;

     DerelictGLFW3.load;

     glfwSetErrorCallback( &onErr );

     // Add the parens here!!!!!!!!
     if ( !glfwInit() )
        return false;

     timer.time = 0.0001;

     return true;
}

Do this and you'll get an error message if any glfw function call fails. 
And always keep in mind that when using Derelict, the library functions 
you call are all function pointers. That means this function:

auto glfwTerm() {return glfwTerminate;}

Is returning a function pointer. It isn't calling anything. You need to 
add parens here, too. In fact, I strongly recommend you use parens 
everywhere unless you are calling properties. Speaking of which, why 
would you make glfwTerm a property?

One more bit of advice. In initGL, you have this:

DerelictGL3.load;
glfwMakeContextCurrent(window.get);
DerelictGL3.reload;

The call to glfwMakeContextCurrent doesn't need to be there. For ease of 
maintenance, I suggest you move it to the method where you call 
glfwCreateWindow. That way, initGL does not depend on the window already 
being created. You don't need to load DerelictGL3 before calling it.

> I'll try a static loading though, sure.

This won't make a difference.




More information about the Digitalmars-d-learn mailing list