Hello all! I'm trying to create a Skybox object, which when loaded loads a single image file and splits it into 5 or 6 (depending on if there's a bottom image) sf::Image objects, represented as a std::vector<sf::Image>. I then pass the image data (from image.getPixelsPtr() ) to glTexImage2D and call a number of other OpenGL functions to set up my textures for rendering.
My issue comes at the end of my function: when the vector is cleared off the stack, it results in a crash dump to stderr and then a SIGABRT crash. If I run it in my debugger, it becomes apparent it's resulting from in the sf::Image destructor. Here's my code (with some irrelevant portions removed):
bool Skybox::loadSkybox() {
glGenTextures(6, ids);
//Loads the image file, then splits into 5-6 images using sf::Image::copy, returning vector of images
//returns a list of skybox images, in order: Top, Bottom?, Left, Right, Front, Back
const std::vector<sf::Image> images = Image->getAsSkybox();
if (images.empty() || images.at(0).getSize().x == 0) //Empty list or empty images is an error with loading
return false;
for (unsigned int i = 0; i < images.size(); i++) {
glBindTexture(GL_TEXTURE_2D, ids[i]);
GLuint width = images.at(i).getSize().x, height = images.at(i).getSize().y;
unsigned char* image = const_cast<unsigned char*>(images.at(i).getPixelsPtr());
if (image == NULL || width <= 0 || height <= 0) {
std::err << "Error loading image file." << std::endl;
glDeleteTextures(6, ids);
return false;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_COMPRESSED_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
GLenum err;
if ((err = glGetError()) != GL_NO_ERROR) {
std::err << "OpenGL error in loading image number " << i << ": " << gluErrorString(err) << std::endl;
glDeleteTextures(6, ids);
return false;
}
//Call other OpenGL functions to set it up
free(image); //Clear our temporary image data
}
return true;
}
I'm using SFML2, Code::Blocks for my IDE and GCC 4.6 on a 32-bit on an Ubuntu 12.04 LTS environment.