memononen/fontstash

Access violation on fons__getState

davidtweaver opened this issue · 8 comments

Trying to incorporate the example code into a project, but when the software makes the call to fonsSetFont, I receive an access violation on fons__getState

I've included a sample of the code below

    struct FONScontext* fs = glfonsCreate(512, 512, FONS_ZERO_TOPLEFT);
int fontNormal = fonsAddFont(fs, "sans", "../DATA/Fonts/apercu-regular.ttf");
fonsSetFont(fs, fontNormal);
fonsSetSize(fs, 64.0f);
fonsDrawText(fs, dx, dy,text, NULL);

Are you checking the return value of fs? It should ne non-null.

Hmm, you're right, it does appear to be null. Have I missed something important?

Thanks for your help by the way.

It can either fail if it cannot allocate memory or if it fails to create texture. Are you initializing OpenGL correctly? If you can run your code in debugger it would help a lot of you could step into the glfonsCreate() (and subsequent call to fonsCreateInternal()) and see where it errors out.

I'm using GLee.h (which apparently doesn't need initialising but I'm calling GLeeInit() to be on the safe side)

Stepping through just provides an unhandled exception at return &stash->states[stash->nstates-1];

in fons__getState

it doesn't move forward from there

The problem happens arealier. During the init, can should step into glfonsCreate() and when in there, step into fonsCreateInternal() and let me know at which line the code exists?

Hi there memononen, it's happening the same here, I followed the glfonsCreateInternal method but I didn't find the problem (I don't know how to use the debugger too well), here's a capture of the fs, I hope it helps, thanks for everything!
error

The debug says that fs = NULL, means that glfonsCreate() failed. Looking at your code the reason it fails is that you have not initialized OpenGL, you have to do that before you call glfonsCreate(). Just put the initialization code after the GLFW window open code and everything should be fine.

Lol, that's awesome man, it worked, thanks again!