2009-03-08 21:35:42 UTC
Ok, here's the altered code for the overloaded = operator. The copy constructor shouldn't be a problem, since it's a constructor, and memory was not allocated to graph_ before it is called.
* * *
// Overloading operator =
/////////////////////////////////////////////////////////
pngwriter & pngwriter::operator = (const pngwriter & rhs)
{
if( this==&rhs)
{
return *this;
}
int original_width = width_; //
int original_height = height_; //
width_ = rhs.width_;
height_ = rhs.height_;
backgroundcolour_ = rhs.backgroundcolour_;
compressionlevel_ = rhs.compressionlevel_;
filegamma_ = rhs.filegamma_;
transformation_ = rhs.transformation_;
delete filename_; //
delete textauthor_; //
delete textdescription_; //
delete textsoftware_; //
delete texttitle_; //
filename_ = new char[strlen(rhs.filename_)+1];
textauthor_ = new char[strlen(rhs.textauthor_)+1];
textdescription_ = new char[strlen(rhs.textdescription_)+1];
textsoftware_ = new char[strlen(rhs.textsoftware_)+1];
texttitle_ = new char[strlen(rhs.texttitle_)+1];
strcpy(textauthor_, rhs.textauthor_);
strcpy(textdescription_, rhs.textdescription_);
strcpy(textsoftware_,rhs.textsoftware_);
strcpy(texttitle_, rhs.texttitle_);
strcpy(filename_, rhs.filename_);
int kkkk;
bit_depth_ = rhs.bit_depth_;
colortype_= rhs.colortype_;
screengamma_ = rhs.screengamma_;
if(original_height != height_ || original_width != width_)
{
// If the height or width have changed, free the memory
for (kkkk = 0; kkkk < height_; kkkk++)
{
if(graph_[kkkk] != NULL) free(graph_[kkkk]);
}
if(graph_ != NULL) free(graph_);
// And re-allocate it
graph_ = (png_bytepp)malloc(height_ * sizeof(png_bytep));
if(graph_ == NULL)
{
std::cerr << " PNGwriter::pngwriter - ERROR **: Not able to allocate memory for image." << std::endl;
}
for (kkkk = 0; kkkk < height_; kkkk++)
{
graph_[kkkk] = (png_bytep)malloc(6*width_ * sizeof(png_byte));
if(graph_[kkkk] == NULL)
{
std::cerr << " PNGwriter::pngwriter - ERROR **: Not able to allocate memory for image." << std::endl;
}
}
}
// At this point, graph_ is the right size for rhs, either because we re-allocated it, or because the sizes were the same.
int tempindex;
for(int hhh = 0; hhh<width_;hhh++)
{
for(int vhhh = 0; vhhh<height_;vhhh++)
{
tempindex=6*hhh;
graph_[vhhh][tempindex] = rhs.graph_[vhhh][tempindex];
graph_[vhhh][tempindex+1] = rhs.graph_[vhhh][tempindex+1];
graph_[vhhh][tempindex+2] = rhs.graph_[vhhh][tempindex+2];
graph_[vhhh][tempindex+3] = rhs.graph_[vhhh][tempindex+3];
graph_[vhhh][tempindex+4] = rhs.graph_[vhhh][tempindex+4];
graph_[vhhh][tempindex+5] = rhs.graph_[vhhh][tempindex+5];
}
}
return *this;
}