Answered You can hire a professional tutor to get the answer.

QUESTION

I have some leaks and segfault but i cant find it. here is my scene.

Hi, I need help fixing some errors... I have some leaks and segfault but i cant find it...here is my scene.cpp file and headerI'll also attach the files in RAR in case if you want to run them :)#include "scene.h"using namespace std;Scene::Scene(int max){ image_array = new Image * [max]; //allocate new memory x_coord = new int[max]; y_coord = new int[max];for(int i=0; i<max;i++){image_array[i]=NULL;x_coord[i]=0;y_coord[i]=0;} maximage=max;}Scene::~Scene(){destructor();}Scene::Scene(Scene const & source){copy(source);}void Scene::destructor(){ for(int i=0;i<maximage;i++){ delete image_array[i]; } delete[] image_array; delete [] x_coord; delete [] y_coord;}void Scene::copy(const Scene& source){ //assume image_array is cleared Image ** image_array = new Image*[source.maximage]; int * x_coord = new int[source.maximage]; int * y_coord = new int[source.maximage]; for(int i=0;i<source.maximage;i++){image_array[i]=source.image_array[i];x_coord[i] = source.x_coord[i];y_coord[i] = source.y_coord[i]; }}Scene const & Scene::operator=(Scene const & source){if(this != &source){ //check for self-assignmentdestructor(); //clear memory assoc. with objcopy(source); //make copy}return *this; //return helpful value}void Scene::addpicture(const char* FileName, int index, int x, int y){if(index>maximage-1||index<0){ //check index out of boundscout << "index out of bounds" << endl;return;}//already existsif(image_array[index] != NULL)delete image_array[index];//add pic,x,yimage_array[index]=new Image();image_array[index]->readFromFile(FileName);x_coord[index]=x;y_coord[index]=y;}void Scene::changemaxlayers(int newmax){//check validfor(int i=0; i<maximage; i++){if(image_array[i]!=NULL&i>newmax-1){ cout << "invalid newmax" << endl; return; }}Image ** new_image_array = new Image * [newmax]; for(int i=0;i<maximage&&i<newmax;i++){if(new_image_array[i]!=NULL)delete new_image_array[i];new_image_array[i]=image_array[i]; } //end for;//pass pointer if(newmax>maximage){ for(int i=maximage;i<newmax;i++)image_array[i]=NULL; }maximage=newmax; image_array = new_image_array;for(int i=0;i<newmax;i++){delete new_image_array[i];}delete [] new_image_array;}void Scene::changelayer(int index, int newindex){if(index>-1&&newindex>-1&&newindex!=index&&index<(maximage-1)&&newindex<(maximage-1)&&image_array[index]!=NULL){ if(image_array[newindex] != NULL)delete image_array[newindex]; image_array[newindex]=image_array[index]; //pass pointer image_array[index]=NULL; }elsecout << "invalid index" << endl;}void Scene::translate(int index, int xcoord, int ycoord){if(index>-1&&index<(maximage-1)&&image_array[index]!=NULL){ x_coord[index] = xcoord; y_coord[index] = ycoord; }elsecout << "invalid index" << endl;}void Scene::deletepicture(int index){ if(index>-1&&index<(maximage-1)){delete image_array[index];image_array[index]=NULL; //fix dangling ptr }else cout << "invalid index" << endl;}Image* Scene::getpicture(int index) const{if(index>-1&&index<(maximage-1)){return image_array[index];}elsecout << "invalid index" << endl;return NULL;}Image Scene::drawscene() const{int x=0;int y=0; for(int i=0; i<maximage;i++){if(image_array[i]!=NULL){int max_width = image_array[i]->width() + x_coord[i];int max_height = image_array[i]->height() + y_coord[i];if(max_width>x)x=max_width;if(max_height>y)y=max_height;}//if }//end for//Image* image= Image();Image image;//image->resize(x,y);image.resize(x,y);for(int i=0; i< maximage;i++ ){ if(image_array[i]!=NULL)for(int j=x_coord[i];j<image_array[i]->width()+x_coord[i];j++){for(int k=y_coord[i];k<image_array[i]->height()+y_coord[i];k++){*(image)(j,k)=*(*image_array[i])(j-x_coord[i],k-y_coord[i]); }//k }//j}//i//Image result = (*image);//delete image;return image;}----------------------------------------------------------scene.h#ifndef SCENE_H#define SCENE_H#include "image.h"using namespace std;class Scene{public:Scene(int max);Scene(Scene const & source);~Scene();void destructor();void copy(const Scene & source);Scene const & operator =(Scene const & source);void addpicture(const char* FileName, int index, int x, int y);void changemaxlayers(int newmax);void changelayer(int index, int newindex);void translate(int index, int xcoord, int ycoord);void deletepicture(int index);Image* getpicture(int index) const;Image drawscene() const;private:Image ** image_array;Image ** new_image_array;int * x_coord;int * y_coord;int maximage;};#endif

Show more
LEARN MORE EFFECTIVELY AND GET BETTER GRADES!
Ask a Question