I have a simple class:
class Histogram {
int m_width;
int m_height;
int m_sampleSize;
int m_bufferWidth;
int m_bufferHeight;
uint8* m_buffer;
int m_size;
public:
Histogram() : m_buffer(0) { }
Histogram(int width, int height, int sampleSize) {
m_buffer = new unsigned char [width*height*sampleSize];
}
~Histogram() {
my_log("destructor: buffer: %p", m_buffer);
if ( m_buffer ) { delete [] m_buffer; m_buffer = NULL; }
}
unsigned char* buffer() {
return m_buffer;
}
};
It is a member in other class:
class Other {
Histogram m_histogram;
void reset() {
my_log("reset() called: buffer: %p", m_histogram.buffer());
m_histogram = Histogram(512, 512, 2);
}
}
Now, I first create "uninitialized" object using Histogram() constructor – which sets m_buffer to NULL;
Then, I call the reset method, which does m_histogram = Histogram( 512, 512, 3 ) – the new object has m_buffer initialized via new.
So expected sequence of log messages is:
- "reset() called: buffer: 0x0"
- "destructor: buffer: 0x0"
But instead, I get:
- "reset() called: buffer: 0x0"
- "destructor: buffer: 0x072a7de"
So some irrational action is being performed. Moreover, the 0x072a7de address is displayied when I also delete the second object (created with "larger" constructor, with three int parameters).