Bad Access Error with ofLoadImage

I’ve been working with openframeworks for a little while now and hadn’t encountered this problem until today. When I use loadImage in ofApp.cpp’s setup function I have no problem and the image loads properly (thus, all of the examples work fine) so I know that the file path is correct. However, when I try to use ofLoadImage in a separate class function I get an EXC_BAD_ACCESS error on line 631 of ofImage.cpp which reads:

ofLogError("ofImage") << "loadImage(): couldn't load image from \"" << fileName << "\"";

which trickles down to line 76 of ofLog.h:

message << value << padding;

and so on. I assumed that this meant there was a problem with the type of the variable I was passing into the ofLoadImage argument but even when I try and call it statically from inside the class function I still get the error. Bizarre. Any help is most appreciated, thanks!

I’ve just discovered that I’m getting the error because I’m calling ofLoadImage from inside a class constructor. Is there any particular reason why I’m not allowed to do that?

You should not do any texture / OpenGL calls in constructors, because openGL might not be setup yet. Try adding a setup function in your class with the load call in it and calling setup on your class from ofApp.

Ah, that makes sense! Thanks!