I have a class (DataBuffer) that has functions that return objects of DataBuffer but when I'm calling other class functions from within the function that returns the object of DataBuffer, the internal values are not being set after the function returns.
So, basically (if you made it through that first sentence), I have an initial DataBuffer that I need to perform various functions on and return new DataBuffers leaving the initial DataBuffer intact. The problem is that in one of the functions that operate on the original DataBuffer the DataBuffer that will be returned is not getting the correct values out of other functions within the DataBuffer class. Specifically setDimensions().
To the code!
DataBuffer.h
class DataException;
class DataBuffer
{
public:
DataBuffer ();
DataBuffer (const DataBuffer &rhs);
DataBuffer (unsigned int width, unsigned int height, float fillValue=0.0)
throw (DataException);
void setDimensions (unsigned int width, unsigned int height, float fillValue=0.0)
throw (DataException);
DataBuffer func ();
protected:
float **allocate (unsigned int width, unsigned int height)
throw (DataException);
unsigned int width;
unsigned int height;
float **buffer;
}
DataBuffer.cpp
DataBuffer::DataBuffer ()
: width (0),
height (0),
buffer (NULL)
{
}
DataBuffer::DataBuffer (const DataBuffer &rhs)
: width (rhs.width),
height (rhs.height),
buffer (rhs.buffer)
{
}
DataBuffer::DataBuffer (unsigned int width, unsigned int height, float *fillValue)
throw (DataException)
{
try
{
buffer = allocate (width, height);
}
catch (DataException &e)
{
e.addToStack ("DataBuffer::DataBuffer (unsigned int, unsigned int, float");
throw e;
}
for (unsinged int i=0 ; i<height ; ++i)
{
for (unsigned int j=0 ; j<width ; ++j)
{
buffer[i][j] = fillValue;
}
}
}
void DataBuffer::setDimensions (unsigned int width, unsigned int height, float fillValue)
throw (DataException)
{
if ((width !=0) && (height != 0))
{
this->width = width;
this->height = height;
try
{
buffer = allocate (width, height);
}
catch (DataException &e)
{
e.addToStack ("DataBuffer::setDimensions()");
throw e;
}
for (unsigned int i=0 ; i<height ; ++i)
{
for (unsigned int j=0 ; j<width ; ++j)
{
buffer[i][j] = fillValue;
}
}
}
else
{
throw DataException ("One of the dimensions is set to zero (0)", "DataBuffer::setDimensions()");
}
}
float** DataBuffer::allocate (unsigned int width, unsigned int height)
throw (DataException)
{
float **result;
...
return result;
}
DataBuffer DataBuffer::func ()
{
DataBuffer result;
try
{
result.setDimensions (this->width, this->height);
}
catch (DataException &e)
{
e.addToStack ("DataBuffer::func()");
throw e;
}
...
return result;
}
My problem is that after calling setDimensions() in func() the values in result don't match what I passed in.
When I step into setDimensions() in the debugger (gdb) the values all get set properly but when it exits, the values in result are changed from what they were before setDimensions() but they aren't what was set in the function.
For example, when I'm debugging I stop at the return.setDimensions() call and step in to setDimensions() with values of width=500 and height=750. I can step through setDimensions() and everything looks fine but after stepping out of setDimensions() and looking at the values in result I see width=3000, height=0 and buffer is a different pointer.
I can't see anything wrong with what I'm doing but there is obviously something incorrect that I am missing . I would be greatly appreciated if someone could help me figure out what I'm doing wrong.
Unfortunately, I am not able to show the details of the allocate() function as it is a proprietary management scheme and the target system does not work with STL or templates so those aren't options. Also I am unable to use the encapsulation principles of c++ due to the extra milliseconds it takes for function calls, thus the values of width, height and buffer are public.
Thanks.
BTW, this is using g++ (GCC) 4.4.5 on Fedora 13