- I doubt anyone else will have this particular problem, but have a discussion topic. This problem has been fixed and was related to the MASSIVELY quirky way that bitmap information is stored and accessed in the Acknex "3D Game Studio" engine. Thanks to those who gave their suggestions. -
Here's a puzzler. Why would a variable assignment crash, but only when trying to assign a useful value?
The project is a video encoder that takes a 3d rendered scene and passes it on to the ffmpeg library as the next frame of a video. Everything is perfectly fine - until this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
void videoDrawFrame(void* finalbits, var wid, var hei)
{
size_t width = _INT(wid);
size_t height = _INT(hei);
for (size_t y = 0;y<height;y++)
{
for (size_t x = 0;x<width;x++)
{
size_t offset = (y*width+x) * 3;
char* c = (char*)(finalbits);
m_VideoRGBFrame[offset+0] = 10; // These work fine, proving it's okay to write to all these addresses
m_VideoRGBFrame[offset+1] = 10;
m_VideoRGBFrame[offset+2] = 10;
// Following is my attempt to debug the problem.
char test = c[2+offset]; // These work fine, proving it's okay to read from all these addresses of the finalbits array.
test = c[1+offset];
test = c[0+offset];
m_VideoRGBFrame[offset+0] = test; // THIS crashes the program! Commenting out this line makes it run (but not do anything useful.)
// The following three lines are what I WANT the program to do. But uncommenting guarantees a crash.
/*m_VideoRGBFrame[offset+0] = c[2+offset];
m_VideoRGBFrame[offset+1] = c[1+offset];
m_VideoRGBFrame[offset+2] = c[0+offset];*/
}
}
// Commenting out this line does not solve the crash, proving that what's done with the data isn't the cause.
MovieEncoderClass->WriteFrame(m_VideoRGBFrame);
}
|
"What kind of code is THAT?" you ask. Here's some information.
- var is a fixed-point number typedef'd to a long, used by the rendering engine. The _INT macro converts it to a valid int. Otherwise not relevant.
- m_VideoRGBFrame is a char* that was previously allocated with a size of (width*height*3), meaning 3 bytes per video pixel. The lines of test code (second comment) prove that the memory was allocated properly.
- finalbits is the raw data of the input bitmap.
- Yes, some offsets are backwards. The input bitmap is presented to me in BGR byte order, while the video encoder expects RGB.
- I have no idea what happened with the indenting. Looks fine in the IDE, but a bit messy here. At least it's got some indenting.
Over the last several years I have written many, many C++ projects, and not ONCE have I ever seen this happen. Does anyone have any thoughts?