So the class I'm working on is really messy at the moment because I'm playing around with it a lot and experimenting. It is a terrain class that loads a height map computes normals, etc, etc. Originally I had it where it would exit cleanly if it was given the name of a height map image that didn't exist, and I guess it still does, but as the class has grown there are problems with destruction.
So I have this public function:
Code:
bool Terrain::loadTerrain(const std::string& source)
{
source_ = source;
if (!hmImage_.loadTextureFromFile(source_)) {
std::cout << "Failed to load heightmap, quitting." << std::endl;
return false;
}
height_ = hmImage_.getHeight();
width_ = hmImage_.getWidth();
data_ = hmImage_.getData();
applyBilinearFilter();
heightMap_.resize(height_);
for (unsigned int i = 0; i < heightMap_.size(); i++) {
heightMap_[i].resize(width_);
}
for (int i = 0; i < height_; i++) {
for (int j = 0; j < width_; j++) {
if (filteredData_[i] != 0)
heightMap_[i][j] = (float)filteredData_[i][j];
else {
std::cout << "When copying filteredData into heightmap a
null pointer was almost accessed. Quitting." << std::endl;
return false;
}
}
}
for (int i = 0; i < width_; i++) {
delete [] filteredData_[i];
filteredData_[i] = 0;
}
delete [] filteredData_;
filteredData_ = 0;
computeNormals();
std::cout << "Generating terrain display list...";
createDisplayList();
std::cout << " Done." << std::endl;
terrainReady = true;
return true;
}
Even though this is a height map, I used my texture class to load the image. I'm pretty sure that the problem is with filteredData_ which is a 2d array of floats. the function called applyBilinearFilter samples data_, which is a pointer to memory reserved by the FreeImage library, and saves the sampled data in filteredData_.
loadTerrain(const std::string& source) is called in my main function like this:
Code:
Terrain terrain;
terrain.setRenderMode(SOLID_MODE | VERTEX_LIGHTING);
if (!terrain.loadTerrain(settings.getMap())) {
glfwTerminate();
std::cout << "dbg" << std::endl;
return 0;
}
When executed dbg is outputted in the console. Also when I debug I get:
Debugger finished with status 0
and the call stack is empty. I'm pretty sure that I'm not explicitly any destructors.
Some searching has given me the idea that this is probably caused when my objects go out of scope and their destuctors are called when main returns. So here it is, the destructor for my terrain class:
Code:
Terrain::~Terrain()
{
if (terrainReady) {
for (unsigned int i = 0; i < heightMap_.size(); i++) {
heightMap_[i].clear();
}
heightMap_.clear();
for (unsigned int i = 0; i < normals_.size(); i++) {
normals_[i].clear();
}
normals_.clear();
}
data_ = 0;
}
I normally avoid using these c style pointer arrays like the plague, so I'm not very experienced with them. Let me know what you think, or if you need to see more code.
Thanks
edit: I should also mention that it works just fine when I don't give it a bad file name.